倒转字符串中字符的顺序 reverse()
用法
reverse(指针,长度)
string: reverse( str.begin() , str.end() );
char[]: reverse( str2 , str2 + len);
int[]: reverse( num , num + n );
View Code
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 string str = "abcdef"; //测试string 8 cout<<str<<endl; 9 reverse(str.begin(),str.end()); 10 cout<<str<<endl; 11 12 13 char str2[110] = "abcdefgh"; //测试char数组 14 int len = strlen(str2); 15 cout<<str2<<endl; 16 reverse(str2,str2 + len); 17 cout<<str2<<endl; 18 19 int n = 5; //测试int数组 20 int num[110]; 21 int i; 22 for(i=0;i<n;i++) 23 { 24 num[i] = i+1; 25 cout<<num[i]; 26 } 27 cout<<endl; 28 reverse(num,num + n); 29 for(i=0;i<n;i++) 30 { 31 cout<<num[i]; 32 } 33 cout<<endl; 34 return 0; 35 }