简体   繁体   English

C ++ - std :: wstring to std :: string - 快速和脏转换,用作std :: map中的键

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

I'm looking for a bit of advice on the best way to convert a std::wstring to std::string - but a quick and dirty conversion for use as a key in an std::map<std::string, int> object. 我正在寻找关于将std::wstring转换为std::string的最佳方法的一些建议 - 但是在std::map<std::string, int>用作键的快速而脏的转换std::map<std::string, int>对象。

The map is quite large, and is already well integrated into the project already, and there are only a handful of keys that require this conversion so I think it will be wasteful to change the map into one that accepts std::wstring as the key. 地图非常大,并且已经很好地集成到项目中,并且只有少数键需要这种转换,所以我认为将地图更改为接受std::wstring作为键的地图将是浪费。

The output of the conversion doesn't really matter, but it has to be consistent so as to reliably pull the correct values from the map every time. 转换的输出并不重要,但必须保持一致,以便每次都可靠地从地图中提取正确的值。

The application is a Windows only application. 该应用程序是仅限Windows的应用程序。

Is there any known process to do a rough conversion reliably for this purpose? 是否有任何已知的过程可靠地为此目的进行粗略转换? Or would the best way be via the usual, proper, conversion process (as described in this SO question/answer: How to convert wstring into string? )? 或者最好的方法是通过通常的,正确的转换过程(如本问题/答案中所述: 如何将wstring转换为字符串? )?

Edit: Please bear in mind - losing information is fine as long as things are consistent. 编辑:请记住 - 只要事情一致,丢失的信息就可以了。 ie If I throw in some Japanese characters, and they consistently convert into the same (potentially garbage) std::string , that's fine. 即如果我输入一些日文字符,并且它们一直转换为相同的(可能是垃圾) std::string ,那很好。 This will never be for display, only to be used as a key to pull out values from a map. 这永远不会用于显示,只能用作从地图中提取值的键。

Thanks! 谢谢!

As a variant, I would go for 作为变种,我会去

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

Maybe the other answer is faster (depends on string iterators' implementation), but this is a more std\\stl way as for me. 也许另一个答案更快(取决于字符串迭代器的实现),但这对我来说是一种更为标准的方式。 But yep, this will lose some unique characters. 但是,这将失去一些独特的角色。

If you are not interested in the semantic of the content, but just to the content to be comparable, I'll just coherce the inner wchar[] into a char[] of doubled size and use it to initialize the string (by specifying address/size in the constructor) 如果你对内容的语义不感兴趣,而只是对可比较的内容感兴趣,我只是将内部wchar []与一个加倍大小的char []结合起来并用它来初始化字符串(通过指定地址) /构造函数中的大小)

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

Now s is unprintable (it may contain null chars) but still assignable and comparable. 现在s是不可打印的(它可能包含空字符)但仍然可分配和可比较。

Yo can go back 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 . 应该打印1

However, note that this way the order in the map (result of operator< ) is ... fuzzy because of the presence of the 0, and don't reflect any text sematics. 但是,请注意,这样地图中的顺序( operator<结果)是...因为存在0而模糊,并且不反映任何文本语义。 However search still works, since -however fuzzy- it is still an "order". 然而搜索仍然有效,因为 - 无论模糊 - 它仍然是一个“秩序”。

You can convert std::wstring to utf-8 (using WideCharToMultiByte or something like this lib: http://utfcpp.sourceforge.net/ ), that is, a null-terminating c-string, and then construct std::string from it. 你可以将std :: wstring转换为utf-8(使用WideCharToMultiByte或者像这样的lib: http//utfcpp.sourceforge.net/ ),也就是一个空终止的c-string,然后构造std :: string从中。 This conversion will be reversible. 这种转换是可逆的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM