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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15333259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:16:52  来源:igfitidea点击:

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

c++stdstring

提问by Jace

I'm looking for a bit of advice on the best way to convert a std::wstringto std::string- but a quick and dirty conversion for use as a key in an std::map<std::string, int>object.

我在寻找到一个转换的最佳途径一些建议std::wstringstd::string-但一个快速和肮脏的转换用作一个关键std::map<std::string, int>对象。

The map is quite large, and is already wellintegrated 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::wstringas 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 asthings are consistent. i.e. If I throw in some Japanese characters, and they consistently convert into the same (potentially garbage) std::string, that's fine. This will never be for display, only to be used as a key to pull out values from a map.

编辑:请记住 -只要事情是一致的,丢失信息就可以了。即如果我输入一些日语字符,并且它们始终转换为相同的(可能是垃圾)std::string,那很好。这永远不会用于显示,只会用作从地图中提取值的键。

Thanks!

谢谢!

回答by Roman Kruglov

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.

也许另一个答案更快(取决于字符串迭代器的实现),但这对我来说是一种更 std\stl 的方式。但是是的,这会丢失一些独特的字符。

回答by Emilio Garavaglia

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[] 并使用它来初始化字符串(通过指定地址/size 在构造函数中)

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

Now sis 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. However search still works, since -however fuzzy- it is still an "order".

但是,请注意,operator<由于存在 0,因此地图中的顺序( 的结果)是 ... 模糊的,并且不反映任何文本语义。然而搜索仍然有效,因为 - 无论多么模糊 - 它仍然是一个“订单”。

回答by user2155932

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. This conversion will be reversible.

您可以将 std::wstring 转换为 utf-8(使用 WideCharToMultiByte 或类似 lib: http://utfcpp.sourceforge.net/ 的东西),即空终止 c 字符串,然后构造 std::string从中。这种转换是可逆的。