How to convert wstring into string?

前端 未结 17 1967
北海茫月
北海茫月 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:17

    Instead of including locale and all that fancy stuff, if you know for FACT your string is convertible just do this:

    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
      wstring w(L"bla");
      string result;
      for(char x : w)
        result += x;
    
      cout << result << '\n';
    }
    

    Live example here

提交回复
热议问题