2017-06-05 302 views
-1

我正在用C++编写一个程序,它必须处理unicode字符。 主要的问题是我使用的算法,我需要通过焦炭解析我的S/wstrings 字符:使用wstring和wcout没有得到预期的输出

std::wstring word = L"Héllo" 
for (auto &e : word) 
// doing something with e 

但是,如果我运行此:

std::wstring word = L"Héllo" 
for (auto &e : word) 
    std::wcout << e << std::endl; 

我得到这样的输出:

H 
? 
l 
l 
o 

我做错了什么?

请注意,当使用std::wcout << word;时,请正确打印word

编辑@Ben福格特

这里是出与std::wcout << std::hex << std::setw(4) << (int)e << L" " << e << L'\n';

48 H 
    e9 � 
    6c l 
    6c l 
    6f o 
+4

你呢r控制台支持unicode? – NathanOliver

+1

观察:不处理代理对,因为未知字符只有一行输出。 –

+0

它的确如此,因为当我打印std :: wcout中的所有字符串时,我可以看到'é' – LightMan

回答

0

要打印的字符,你打算尝试以下操作:

#include <string> 
#include <iostream> 
#include <fcntl.h> 
#include <io.h> 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    std::wstring word = L"Héllo"; 
    _setmode(_fileno(stdout), _O_U16TEXT); 
    for (auto &e : word) 
     std::wcout << e << std::endl; 
    return 0; 
} 

更详细的说明在这篇文章中:Why are certain Unicode characters causing std::wcout to fail in a console app?

+1

这些函数不存在[在Linux上](https://stackoverflow.com/questions/44372286/using-wstring-and-wcout-doesnt-get-the-expected-output#comment75745817_44372286)他们呢? –

相关问题