库特<< "привет" ;或 wcout<< L "привет" ;
cout<< "привет"; or wcout<< L"привет";
Why
cout<< "привет";
工作良好,而
wcout<< L"привет";
不?(在 Qt Creator for Linux 中)
GCC和Clang默认将源文件视为UTF-8。您的 Linux 终端很可能也配置为 UTF-8。因此,有了cout<< "привет"
有一个 UTF-8 字符串,它打印在 UTF-8 终端中,一切都很好。
wcout<< L"привет"
取决于正确的区域设置配置,以便将宽字符转换为终端的字符编码。需要初始化区域设置才能使转换正常工作(默认的"经典"又名"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";
}
有了g++ test.cc && ./a.out
,这打印了"привет"(在Debian Jessie上)。
另请参阅此答案,了解在标准输出中使用宽字符的危险。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- wcout不会打印正确的单引号
- 使用std::wcout会降低奇怪的性能
- Printing CComBSTR with iostream (std::wcout)
- 这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
- 如何C++ WCOUT UTF-16 编码的字符数组?
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 使用 wstring 和 wcout 无法获得预期的输出
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 将char8_t const* 输出到 cout 和 wcout,一个编译一个不编译
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败