use*_*077 11 c++ linux unicode console-application
为什么
cout<< "??????";
Run Code Online (Sandbox Code Playgroud)
效果很好
wcout<< L"??????";
Run Code Online (Sandbox Code Playgroud)
才不是?(在Qt Creator for linux中)
Art*_*mGr 15
GCC和Clang默认将源文件视为UTF-8.您的Linux终端很可能也配置为UTF-8.所以cout<< "??????"有一个UTF-8字符串,用UTF-8终端打印,一切都很好.
wcout<< L"??????"取决于正确的Locale配置,以便将宽字符转换为终端的字符编码.需要初始化Locale才能使转换工作(默认的"经典"又称"C"语言环境不知道如何转换宽字符).使用std::locale::global (std::locale (""))的区域设置以匹配环境配置或std::locale::global (std::locale ("en_US.UTF-8"))使用特定的语言环境(类似于本C示例).
这是工作计划的完整来源:
#include <iostream>
#include <locale>
using namespace std;
int main() {
std::locale::global (std::locale ("en_US.UTF-8"));
wcout << L"??????\n";
}
Run Code Online (Sandbox Code Playgroud)
用g++ test.cc && ./a.out这个打印"привет"(在Debian Jessie上).
另请参阅此答案,了解在标准输出中使用宽字符的危险.