我想弄清楚如何反转字符串temp
当我以二进制数读取字符串时
istream& operator >>(istream& dat1d, binary& b1)
{
string temp;
dat1d >> temp;
}
我不确定包含二进制数字的字符串是什么意思。但要反转字符串(或任何 STL 兼容容器),您可以使用std::reverse()
. std::reverse()
就地操作,因此您可能需要先复制该字符串:
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string foo("foo");
std::string copy(foo);
std::cout << foo << '\n' << copy << '\n';
std::reverse(copy.begin(), copy.end());
std::cout << foo << '\n' << copy << '\n';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)