C ++ - std :: wstring to std :: string - quick and dirty conversion for use as a key in std :: map

I am looking for some tips on how to convert std::wstring to std::string , but a quick and dirty conversion to use as the key in the std::map<std::string, int> object.

The map is quite large and already well integrated into the project, and there are only a few keys that require this conversion, so I think it will be wasteful to change the map to one, which takes std::wstring as a key.

The result of the conversion does not matter much, but it must be consistent in order to reliably pull the correct values ​​from the map every time.

The application is a Windows-only application.

Is there any known process for crude conversion for this purpose? Or is it best to use the usual, correct conversion process (as described in this SO question / answer: How to convert wstring to string? )?

Edit: Please keep in mind - information loss in order is not yet compatible. those. if I find some Japanese characters and they are successively converted to the same (potentially garbage) std::string , that's fine. It will never be displayed, only to be used as a key to pull values ​​from the map.

Thanks!

+6
source share
3 answers

If you are not interested in the semantics of the content, but just so that the content is comparable, I just bind the internal wchar [] to a double size char [] and use it to initialize the string (specifying the address / size in the constructor)

 std::wstring ws(L"ABCD€FG"); std::string s((const char*)&ws[0], sizeof(wchar_t)/sizeof(char)*ws.size()); 

Now s printable (it can contain null characters), but you can still assign and match.

Yo may return as:

 std::wstring nws((const wchar_t*)&s[0], sizeof(char)/sizeof(wchar_t)*s.size()); 

Now compare

 std::cout << (nws==ws) 

should print 1 .

However, note that in this way the order on the map (the result of operator< ) ... is fuzzy due to the presence of 0 and does not reflect any text sample. However, the search still works, because when it is fuzzy, it is still "order."

+7
source

As an option, I would go for

 std::wstring w( L"Some" ); std::string s( w.begin(), w.end() ); 

Perhaps the other answer is faster (it depends on the implementation of string iterators), but this is the more standard std \ stl method. But yes, it will lose some unique characters.

+9
source

You can convert std :: wstring to utf-8 (using WideCharToMultiByte or something like this lib: http://utfcpp.sourceforge.net/ ), that is, a null ending c-string, and then build std from it: : string. This conversion will be reversible.

+7
source

All Articles