gpt4 book ai didi

c++ - 如何在 linux 上的 wstring 中存储 unicode 字符?

转载 作者:行者123 更新时间:2023-11-30 01:48:35 25 4
gpt4 key购买 nike

#include <iostream>
using namespace std;

int main() {
std::wstring str = L"\u00A2";
std::wcout << str;
return 0;
}

为什么这不起作用?以及如何解决这个问题?

最佳答案

它不起作用,因为在默认的 C 语言环境中,没有对应于 U+00A2 的字符。

如果您使用标准的 ubuntu 安装,您的用户区域设置很可能使用比 US-ASCII 更大的字符集,很可能是使用 UTF-8 编码的 Unicode。所以你只需要切换到环境中指定的语言环境,如下:

#include <iostream>
/* locale is needed for std::setlocale */
#include <locale>
#include <string>

int main() {
/* The following switches to the locale specified
* by the LC_ALL environment variable.
*/
std::setlocale (LC_ALL, "");
std::wstring str = L"\u00A2";
std::wcout << str;
return 0;
}

如果您使用 std::string 而不是 std::wstringstd::cout 而不是 std::wcout,那么您就不需要 setlocale,因为不需要转换(前提是控制台需要 UTF-8)。

关于c++ - 如何在 linux 上的 wstring 中存储 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292932/

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