reverse()函数的使用

reverse()函数可以对字符串进行反转操作,头文件是#include<algorithm>

容器类型的要用begin()和end()来指定反转的区域,数组类型的直接用int类型即可

下面贴出示范代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    string s="hello";
    reverse(s.begin(),s.end());
    char c[]="hello";
    reverse(c,c+strlen(c));
    char x[6]={'h','e','l','l','o'};
    reverse(x,x+strlen(x));
    char str[11];
    for(int i=0;i<5;i++)cin>>str[i];
    reverse(str,str+5);
    cout<<s<<endl;
    cout<<c<<endl;
    cout<<x<<endl;
    for(int i=0;i<5;i++)cout<<str[i];
    return 0;
}

输入"hello"后的输出结果:

猜你喜欢

转载自www.cnblogs.com/wjw2018/p/9313228.html
今日推荐