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"后的輸出結果: