在C ++中处理UTF-8 [英] Handling UTF-8 in C++

查看:156
本文介绍了在C ++中处理UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要想知道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



看看第一个答案。



  1. 当我应该使用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.

std::wstring VS std::string

Take a look at first answer. I'm sure it answers your question.

  1. When I should use std::wstring over std::string?

On Linux? Almost never (§).

On Windows? Almost always (§).

这篇关于在C ++中处理UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆