std::wcout 打印 unicode 字符但它们被隐藏

Elt*_*ets 3 c++ unicode

所以,下面的代码:

\n
#include <iostream>\n#include <string>\n#include <io.h>\n#include <fcntl.h>\n#include <codecvt>\n\nint main()\n{\n    setlocale(LC_ALL, "");\n\n    std::wstring a;\n    std::wcout << L"Type a string: " << std::endl;\n    std::getline(std::wcin, a);\n    std::wcout << a << std::endl;\n    getchar();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当我输入“\xc3\xa5\xc3\xa4\xc3\xb6”时,我得到一些奇怪的输出。终端的光标是缩进的,但后面没有文本。如果我使用右箭头键向前移动光标,则当我单击右箭头键时,“\xc3\xa5\xc3\xa4\xc3\xb6”会显示出来。

\n

如果我包含英文字母,以便输入为“hello\xc3\xa5\xc3\xa4\xc3\xb6”,则输出为“hello”,但当我单击右箭头键“hello\xc3\xa5\xc3\xa4\”时xc3\xb6" 一个字母一个字母地出现。

\n

为什么会发生这种情况,更重要的是我该如何解决它?

\n

编辑:我在 Windows 上使用 Visual Studio 的编译器进行编译。当我在 repl.it 中尝试这个确切的代码(他们使用 clang)时,它就像一个魅力。问题是由我的代码、Windows 还是 Visual Studio 引起的吗?

\n

Mar*_*nen 5

Windows 需要一些特定于操作系统的调用来设置 Unicode 控制台:

\n
#include <iostream>\n#include <string>\n#include <io.h>\n#include <fcntl.h>\n\n// From fctrl.h:\n//  #define _O_U16TEXT     0x20000 // file mode is UTF16 no BOM (translated)\n//  #define _O_WTEXT       0x10000 // file mode is UTF16 (translated)\n\nint main()\n{\n    _setmode(_fileno(stdout), _O_WTEXT); // or _O_U16TEXT, either work\n    _setmode(_fileno(stdin), _O_WTEXT);\n\n    std::wstring a;\n    std::wcout << L"Type a string: ";\n    std::getline(std::wcin, a);\n    std::wcout << a << std::endl;\n    getwchar();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
Type a string: hello\xc3\xa5\xc3\xa4\xc3\xb6\xe9\xa9\xac\xe5\x85\x8b\nhello\xc3\xa5\xc3\xa4\xc3\xb6\xe9\xa9\xac\xe5\x85\x8b\n\n
Run Code Online (Sandbox Code Playgroud)\n