c++ - std::map::find doesn't work -


i'm trying keep information in std::map. have problem find:

typedef map<string, string, equal_to<string>> map_string2string; .... map_string2string map; map_string2string::const_iterator iter; 

when try find key, following error:

iter = map.find(key); 

error visual c++
what doing wrong?
this error appears when have in map.

your map has wrong kind of comparison functor. need strict weak ordering (less-than or greater-than type comparison), not equality. can omit comparison functor parameter , use less-than comparison std::string. implements strict weak ordering via lexicographical comparison of strings:

typedef map<string, string> map_string2string; 

this equivalent to

typedef map<string, string, less<string> > map_string2string; 

internally, map uses strict weak ordering comparison both order itself, , determine whether 2 keys equal.

the third template parameter allows instantiate map custom ordering criterion. example, create map reverse ordering of 1 above:

typedef map<string, string, greater<string> > map_string2string; 

Comments