3
我需要反转wstring。我有这样的代码:C++中的反向wstring
#include <iostream>
#include <string>
#include <locale>
int main() {
std::wstring s;
std::getline(std::wcin, s);
for (const auto &i : s) {
std::wcout << (int) i << " ";
}
std::wcout << std::endl;
std::wcout << s << std::endl;
std::reverse(s.begin(), s.end());
std::wcout << s << std::endl;
return 0;
}
ANSI字符在1个字节编码,我可以很容易地扭转他们:
echo -n "papa" | ./reverse
112 97 112 97
papa
apap
但是,当我进入西里尔文字,被编码超过1个字节,我得到这样的输出:
echo -n "папа" | ./reverse
208 191 208 176 208 191 208 176
папа
�пап�
如何正确地反转该字符串?
P.S.我正在使用OS X.
如果编码是utf-8,那么为什么要使用宽字符类型? – user2079303
@ user2079303使用'std :: string'我得到这样的输出:'-48 -65 -48 -80 -48 -65 -48 -80 папа пап ' – 0x1337
是的,我并不是暗示那是你的问题,只是一个混乱。 – user2079303