2017-06-05 296 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)他們呢? –

相關問題