4

我正在尝试使用 unicode 字符在终端中绘制一些简单的方框图。但是我注意到 wcout 不会为方框图字符输出任何内容,甚至不会输出占位符。所以我决定编写下面的程序并找出支持哪些 unicode 字符,发现 wcout 拒绝输出任何高于 255 的内容。我需要做些什么才能使 wcout 正常工作吗?为什么不能访问任何扩展的 unicode 字符?

#include <wchar.h>
#include <locale>
#include <iostream>

using namespace std;

int main()
{
    for (wchar_t c = 0; c < 0xFFFF; c++)
    {
        cout << "Iteration " << (int)c << endl;
        wcout << c << endl << endl;
    }

    return 0;
}
4

1 回答 1

5

我不推荐使用wcout,因为它不可移植、效率低下(总是执行转码)并且不支持所有 Unicode(例如代理对)。

相反,您可以使用开源 {fmt} 库来便携式打印 Unicode 文本,包括绘图字符,例如:

#include <fmt/core.h>

int main() {
  fmt::print("┌────────────────────┐\n"
             "│   Hello, world!    │\n"
             "└────────────────────┘\n");
}

打印(https://godbolt.org/z/4EP6Yo):

┌────────────────────┐
│   Hello, world!    │
└────────────────────┘

免责声明:我是 {fmt} 的作者。

于 2020-12-28T14:54:58.570 回答