C++语言中反转字符串的函数strrev(), reverse()

1.使用string.h中的strrev函数

 

复制代码
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5     char s[]="hello";
6     strrev(s);
7     puts(s);
8     return 0;
9 }
复制代码

 

2.使用algorithm中的reverse函数

复制代码
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 using namespace std;
 5 int main()
 6 {
 7     string s= "hello";
 8     reverse(s.begin(),s.end());
 9     cout<<s<<endl;
10     return 0;
11 }
复制代码

这两个函数在我测试的时候出现了两种完全不同的情况

1.strrev函数只对字符数组有效,对string类型是无效的。

2.reverse函数是反转容器中的内容,对字符数组无效。

本文作者:王陸

本文链接:https://www.cnblogs.com/wkfvawl/p/9053011.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   王陸  阅读(37786)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起