码迷,mamicode.com
首页 > 其他好文 > 详细

reverse()函数的使用

时间:2018-07-15 13:01:14      阅读:773      评论:0      收藏:0      [点我收藏+]

标签:com   类型   clu   cst   image   int   return   for   cout   

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()函数的使用

标签:com   类型   clu   cst   image   int   return   for   cout   

原文地址:https://www.cnblogs.com/wjw2018/p/9313228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!