【方法】使C++的cout能在不设置locale的情况下正确输出wchar_t Unicode字符串

来源:互联网 发布:手机淘宝松子类目 编辑:程序博客网 时间:2024/05/29 08:25
通过重载cout的<<运算符即可实现。
【程序】
#include <iostream>#include <Windows.h>using namespace std;// 使C++的cout能输出Unicode字符串ostream &operator << (ostream &os, const wchar_t *wstr){    if (os == cout)        WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wstr, wcslen(wstr), NULL, NULL);    return os;}int main(void){    cout << L"简体中文abc" << endl << L"¿Cómo estás?" << endl;    return 0;}


【运行结果】


0 0