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

reverse()函数的使用

 

相关文章:

  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
  • 2021-05-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-25
  • 2022-02-05
  • 2021-07-03
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案