使用std :: string作为std :: map的键 [英] Using an std::string as a key for an std::map

查看:243
本文介绍了使用std :: string作为std :: map的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个std :: map(int .NET 4.0)。我们当然知道地图是一棵树,那个字符串没有为我们定义。


错误24错误C2676:二进制'<':'const std :: string'不定义此运算符或转换为类型可以预先定义的操作符c:\program files\microsoft visual studio 10.0 \vc\include\xfunctional 125 1 FXCMMarketDataServer


所以我把我的google-foo工作,找到这个解决方案:

  struct StringComparerForMap 
{
public:
bool operator()(const std :: string x,const std :: string y)
{
//添加比较逻辑
}
};

...
std :: map< std :: string,CustomObject,StringComparerForMap> MYMAP;

这工作正常了一段时间,现在我遇到了一个我认为是由这个。在STL框架的某个地方,似乎忽略了上述定义,默认为运算符<。



VS2010 .NET 4.0中是否有一种方法来使用字符串作为地图的关键?



我明白,我可以拿这个字符串并写一个函数将其哈希到一个int,但那里的乐趣在哪里? p>

编辑



我将尽力为大卫做最好的解释。当映射使用比较器结构体时,它在释放中崩溃,并且在调试中失败了调试断言。断言失败的是xtree行1746.


表达式:无效运算符<



|中止| |重试| |忽略|


这是什么让我相信,尽管给了地图一个比较器,但是仍然下降某些路径默认为运算符&作比较。我的代码中导致这个的行是:

  CustomObject o = stringObjectMap [key]; 


解决方案


错误24错误C2676 :binary'<':'const std :: string'不定义此运算符或转换为预定义运算符可接受的类型c:\program files\microsoft visual studio 10.0\vc\include\ xfunctional 125 1 FXCMMarketDataServer


当您忘记包含< string> 。那个标题肯定定义了这个操作符。


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.

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

So I put my google-foo to work and found this solution:

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<.

Is there a way in VS2010 .NET 4.0 to use a string as the key of a map?

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?

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.

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:

CustomObject o = stringObjectMap[key];

解决方案

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

That's what VC spits into your face when you forgot to include <string>. That header definitely defines this operator.

这篇关于使用std :: string作为std :: map的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆