C++ 使用 std::string 作为 std::map 的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4939204/
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
Using an std::string as a key for an std::map
提问by Steve H.
I would like to have an std::map (int .NET 4.0). We of course know that a map is a tree and requires an operator< that string does not define for us.
我想要一个 std::map (int .NET 4.0)。我们当然知道地图是一棵树,并且需要一个运算符<,该字符串没有为我们定义。
Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
错误 24 错误 C2676:二进制“<”:“const std::string”未定义此运算符或转换为预定义运算符可接受的类型 c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
So I put my google-foo to work and found this solution:
所以我让我的 google-foo 工作并找到了这个解决方案:
struct StringComparerForMap
{
public:
bool operator()(const std::string x, const std::string y)
{
// Add compare logic here
}
};
...
std::map<std::string, CustomObject, StringComparerForMap> myMap;
This worked fine for a while, and now I'm encountering a bug that I believe is due to this. Somewhere deep down in the STL framework it would seem that it ignores the above definition and defaults to operator<.
这在一段时间内运行良好,现在我遇到了一个错误,我认为这是由于这个原因造成的。在 STL 框架的深处,它似乎忽略了上述定义并默认为 operator<。
Is there a way in VS2010 .NET 4.0 to use a string as the key of a map?
VS2010 .NET 4.0 有没有办法使用字符串作为地图的键?
I understand that I can take that string and write a function to hash it to an int, but where's the fun in that?
我知道我可以使用该字符串并编写一个函数将它散列到一个 int 中,但是这有什么乐趣呢?
EDIT
编辑
I will try and explain this as best I can for David. When the map uses the comparer struct, it crashes in release and fails a debug assertion in debug. The assert that fails is in xtree line 1746.
我会尽力为大卫解释这一点。当映射使用比较器结构时,它会在发布时崩溃并且在调试时无法进行调试断言。失败的断言在 xtree 第 1746 行。
Expression: invalid operator<
|Abort| |Retry| |Ignore|
表达式:无效运算符<
|中止| |重试| |忽略|
That is what leads me to believe that despite giving map a comparer, it still down certain paths defaults to operator< for comparison. The line in my code that causes this is:
这就是让我相信,尽管给 map 一个比较器,它仍然在某些路径下默认为 operator< 进行比较。我的代码中导致这种情况的行是:
CustomObject o = stringObjectMap[key];
回答by sbi
Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
错误 24 错误 C2676:二进制“<”:“const std::string”未定义此运算符或转换为预定义运算符可接受的类型 c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer
That's what VC spits into your face when you forgot to include <string>
. That header definitely defines this operator.
当您忘记包含<string>
. 该标头明确定义了此运算符。