Using an std::string as a key for an std::map
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.
Related videos on Youtube
Steve H.
I am a software developer in Chicago's high-tech trading industry and a Purdue graduate. I create low-latency, high-speed software to trade equities, derivitives, and other instruments electronically.
Updated on February 10, 2020Comments
-
Steve H. about 4 years
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];
-
Keith about 13 yearsstd::string does have a suitable definition of the required operator. Are you certain you have included the <string> header? I think I have seen similar issues when I failed to include it; forward declares of std::string mean that its name itself is known.
-
Chris Card about 13 years.NET has nothing to do with std::map or std::string
-
David Rodríguez - dribeas about 13 yearsWhat makes you think that deep inside the library layers it defaults to
<
? If it were so, it would be broken and you should file a bug report, but most probably you are misinterpreting the data --that error would get triggered in many other situations, someone would probably have detected it before... When I told my first boss that the compiler was wrong he smiled and said: 99% of the time, the problem is located between the chair and the keyboard and not in the compiler
-
-
Joseph almost 11 yearsThis just happened to me, spent a hour playing with maps and keep getting this error over and over. I thought "Why does every example on the internet fail!". It turns out I forgot a darn string header. I feel kinda silly now....