Map Definition: map <Key, Data, Compare, Alloc>
If both last template parameters are also default:
For comparison: less <& Key GT;
Alloc: & Distributor l; value_type>
When entering new values ββinto the map. The new value (valueToInsert) is compared with the old values ββin order ( NB This is not a sequential search, the standard guarantees the maximum complexity of inserting O (log (N)) until Compare (value, ValueToInsert) returns true. Because you are using 'const char *' as the key. The Compare object uses less <const char *> , this class simply executes <on two values. This way you are comparing the values ββof the pointer (not the string), so the order is random (since you don't know where the compiler will put the strings.
There are two possible solutions:
- Change the key type so that it compares string values.
- Define another type of comparison that does what you need.
Personally, I (like Chris) just used std :: string because the <statement used for strings returns a comparison based on the contents of the string. But for arguments, we can simply determine the type of comparison.
struct StringLess { bool operator()(const char* const& left,const char* const& right) const { return strcmp(left,right) < 0; } };
source share