cout < “привет”;或wcout < L“привет”; [英] cout&lt;&lt; &quot;привет&quot;; or wcout&lt;&lt; L&quot;привет&quot;;

查看:197
本文介绍了cout < “привет”;或wcout < L“привет”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

cout<< "привет";

适用于

wcout<< L"привет";

不会? (在Qt Creator for linux)

does not? (in Qt Creator for linux)

推荐答案

GCC和Clang默认将源文件视为UTF-8。您的Linux终端很可能配置为UTF-8。所以用 cout<< привет有一个UTF-8字符串,打印在UTF-8终端,一切都很好。

GCC and Clang defaults to treat the source file as UTF-8. Your Linux terminal is most probably configured to UTF-8 as well. So with cout<< "привет" there is a UTF-8 string which is printed in a UTF-8 terminal, all is well.

取决于正确的区域设置配置,以便将宽字​​符转换为终端的字符编码。区域设置需要初始化才能使转换生效(默认的经典或C语言环境) 不知道如何转换宽字符)。使用 std :: locale :: global(std :: locale())为Locale匹配环境配置或 std :: locale :: global(std :: locale(en_US.UTF-8))以使用特定的区域设置(类似于此C示例)。

wcout<< L"привет" depends on a proper Locale configuration in order to convert the wide characters into the terminal's character encoding. The Locale needs to be initialized in order for the conversion to work (the default "classic" aka "C" locale doesn't know how to convert the wide characters). Use std::locale::global (std::locale ("")) for the Locale to match the environment configuration or std::locale::global (std::locale ("en_US.UTF-8")) to use a specific Locale (similar to this C example).

以下是工作程序的完整源代码:

Here's the full source of the working program:

#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上)。

With g++ test.cc && ./a.out this prints "привет" (on Debian Jessie).

另请参见这个回答关于使用宽字符与标准输出的危险。

See also this answer about dangers of using wide characters with standard output.

这篇关于cout < “привет”;或wcout < L“привет”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆