How to convert wstring into string?

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

    You might as well just use the ctype facet's narrow method directly:

    #include 
    #include 
    #include 
    #include 
    
    inline std::string narrow(std::wstring const& text)
    {
        std::locale const loc("");
        wchar_t const* from = text.c_str();
        std::size_t const len = text.size();
        std::vector buffer(len + 1);
        std::use_facet >(loc).narrow(from, from + len, '_', &buffer[0]);
        return std::string(&buffer[0], &buffer[len]);
    }
    

提交回复
热议问题