使用反向迭代器在C ++中反转字符串? [英] Reversing a string in C++ using a reverse iterator?
本文介绍了使用反向迭代器在C ++中反转字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,我似乎无法找到一种方法来扭转字符串:
I have the following code and I just can't seem to figure out a way to get the strings reversed here:
stringstream convert;
string y="";
string z="";
convert << x;
string::reverse_iterator rit;
y=convert.str();
int j=0;
for (rit = y.rbegin(); rit < y.rend(); rit++){
z[j] = *rit;
j++;
}
有人可以帮我解决这个问题吗?谢谢!
Can someone help me out with this? Thanks!
推荐答案
z.assign(y.rbegin(), y.rend());
或者你可以在施工时这样做:
Or you can do it upon construction:
std::string z(y.rbegin(), y.rend());
如果要修改字符串,请使用std :: reverse:
If you want to modify a string in place, use std::reverse:
std::reverse(y.begin(), y.end());
这篇关于使用反向迭代器在C ++中反转字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文