gpt4 book ai didi

macos - Mac OSX 上的 wcout 问题

转载 作者:行者123 更新时间:2023-12-03 22:54:16 27 4
gpt4 key购买 nike

我正在尝试使用 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;
}

最佳答案

我不推荐使用 wcout因为它不可移植、效率低下(总是执行转码)并且不支持所有 Unicode(例如代理对)。
相反,您可以使用 the open-source {fmt} library可移植打印 Unicode 文本,包括 box drawing characters , 例如:

#include <fmt/core.h>

int main() {
fmt::print("┌────────────────────┐\n"
"│ Hello, world! │\n"
"└────────────────────┘\n");
}
打印( https://godbolt.org/z/4EP6Yo):
┌────────────────────┐
│ Hello, world! │
└────────────────────┘
免责声明 : 我是 {fmt} 的作者。

关于macos - Mac OSX 上的 wcout 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com