2017-06-05 4 views
-1

私はユニコード文字を処理するC++プログラムを作成しています。 主な問題は、私は私がcharで私の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フォークト

FOR

EDITはstd::wcout << std::hex << std::setw(4) << (int)e << L" " << e << L'\n'; とアウトです:

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

rコンソールはユニコードをサポートしていますか? – NathanOliver

+1

観察:サロゲートペアを扱っていない。なぜなら、未知の文字に対して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)彼らはそうですか? –

関連する問題