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

std::wcout printing unicode characters but they are hidden

所以,下面的代码:

#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>
#include <codecvt>

int main()
{
    setlocale(LC_ALL, "");

    std::wstring a;
    std::wcout << L"Type a string: " << std::endl;
    std::getline(std::wcin, a);
    std::wcout << a << std::endl;
    getchar();
}

当我输入“åäö”时,我得到了一些奇怪的输出。终端的光标是缩进的,但后面没有文字。如果我使用右箭头键将光标向前移动,“åäö”会在我单击右箭头键时自行显示。

如果我包含英文字母以便输入为“helloåäö”,则输出为“hello”,但当我单击右箭头键时,“helloåäö”会一个字母一个字母地出现。

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

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

Windows 需要一些 OS 特定的调用来为 Unicode 设置控制台:

#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout),  _O_U16TEXT);
    _setmode(_fileno(stdin), _O_WTEXT);

    std::wstring a;
    std::wcout << L"Type a string: ";
    std::getline(std::wcin, a);
    std::wcout << a << std::endl;
    getwchar();
}

输出:

Type a string: helloåäö马克
helloåäö马克