1.C++ string 使用 reverse_iterator,rbegin and rend;原字符串不变,只是输出。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string tmp("abcdefg");
string::reverse_iterator tmp_it=tmp.rbegin();
for (;tmp_it!=tmp.rend();tmp_it++)
{
cout<<*tmp_it;
}
getchar();
}
2.c++ 反转到一个新的string 中。
int main()
{
string tmp("abcdefg");
string reserve;
string::reverse_iterator tmp_it=tmp.rbegin();
for (;tmp_it!=tmp.rend();tmp_it++)
{
reserve.push_back(*tmp_it);
}
cout<<reserve<<endl;
getchar();
}
3.c++ 原字符串反转。
int main()
{
string tmp("abcdefg");
string::reverse_iterator tmp_it_=tmp.rbegin();
string::iterator tmp_it=tmp.begin();
size_t size_ =tmp.size()/2;
cout<<size_<<endl;
while(size_!=0)
{
char t = *tmp_it;
*tmp_it = *tmp_it_;
*tmp_it_ = t;
tmp_it++;
tmp_it_++;
size_--;
}
cout<<tmp<<endl;
getchar();
}