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"后的输出结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM