Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 filesmicrosoft visual studio 10.0vcincludexfunctional 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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
242 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 filesmicrosoft visual studio 10.0vcincludexfunctional 125 1 FXCMMarketDataServer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...