C++方便的字符串翻转函数reverse

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yuta233/article/details/86622509

1.对于用char定义的字符串:使用string.h中的strrev函数

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char s[]="123456";//不能是string类型;
    strrev(s);
    cout<<s<<endl;
    return 0;
}

2.对于string类型的:使用algorithm中的reverse函数

#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string s[]="123456";
    reverse(s.begin(),s.end());
    cout<<s<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Yuta233/article/details/86622509