在C ++中处理UTF-8 [英] Handling UTF-8 in C++
问题描述
要想知道C ++是否是我的一个项目的正确语言,我想测试UTF-8的能力。根据参考,我建立这个例子:
#include< string>
#include< iostream>
using namespace std;
int main(){
wstring str;
while(getline(wcin,str)){
wcout<< str<< endl;
if(str.empty())break;
}
return 0;但是当我键入一个UTF-8字符时,它会出现错误:
$> ./utf8
Hello
Hello
für
f
$>
不仅不打印
ü
,但也立即退出。gdb
告诉我没有崩溃,但是正常退出,但我发现很难相信。解决方案不要在Linux上使用wstring。
std :: wstring VS std :: string
看看第一个答案。
- 当我应该使用std :: wstring over std :: string?
在Linux上?几乎从不 (§)。
在Windows上?几乎总是(§)。
To find out if C++ is the right language for a project of mine, I wanna test the UTF-8 capabilities. According to references, I built this example:
#include <string> #include <iostream> using namespace std; int main() { wstring str; while(getline(wcin, str)) { wcout << str << endl; if(str.empty()) break; } return 0; }
But when I type in an UTF-8 character, it misbehaves:
$ > ./utf8 Hello Hello für f $ >
Not only it doesn't print the
ü
, but also quits immediately.gdb
told me there was no crash, but a normal exit, yet I find that hard to believe.解决方案Don't use wstring on Linux.
Take a look at first answer. I'm sure it answers your question.
- When I should use std::wstring over std::string?
On Linux? Almost never (§).
On Windows? Almost always (§).
这篇关于在C ++中处理UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!