How to initialize and print a std::wstring?

匆匆过客 提交于 2019-11-28 20:02:31

To display a wstring you also need a wide version of cout - wcout.

std::wstring st = L"SomeText";
...
std::wcout << st; 

Use std::wcout instead of std::cout.

This answer apply to "C++/CLI" tag, and related Windows C++ console.

If you got multi-bytes characters in std::wstring, two more things need to be done to make it work:

  1. Include headers
    #include <io.h>
    #include <fcntl.h>
  2. Set stdout mode
    _setmode(_fileno(stdout), _O_U16TEXT)

Result:

try to use use std::wcout<<st it will fix your problem.

std::wstring st = "SomeText";
...
std::wcout << st;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!