How to convert wstring into string?

前端 未结 17 2170
北海茫月
北海茫月 2020-11-22 13:10

The question is how to convert wstring to string?

I have next example :

#include 
#include 

int main()
{
    std::wstr         


        
17条回答
  •  心在旅途
    2020-11-22 13:23

    At the time of writing this answer, the number one google search for "convert string wstring" would land you on this page. My answer shows how to convert string to wstring, although this is NOT the actual question, and I should probably delete this answer but that is considered bad form. You may want to jump to this StackOverflow answer, which is now higher ranked than this page.


    Here's a way to combining string, wstring and mixed string constants to wstring. Use the wstringstream class.

    #include 
    
    std::string narrow = "narrow";
    std::wstring wide = "wide";
    
    std::wstringstream cls;
    cls << " abc " << narrow.c_str() << L" def " << wide.c_str();
    std::wstring total= cls.str();
    

提交回复
热议问题