Dennis wrote:
What I really meant to say was it is like a map<string,int> rather than
any special class I wrote myself. The description of key_compare is a
"Function object that compares two keys for ordering". So does that
mean it's a class within the map<string,int> that key_comp() uses?
Not quite. In order to illustrate the significance of key_comp, let me take
a little detour and recall the way a map is typically implemented. Usually,
std::map< key_type, mapped_type > uses a (balanced) tree whose nodes store
pairs
std::pair< key_type const, mapped_type >
In order to find efficiently a node for a given key, the tree is organized
as a search tree, i.e., for any given node V, the left subtree has keys
less than the key at V and the right subtree only hosts keys not smaller
than the key at V.
Now, in order to compare keys, the map container uses a comparison function.
This function defaults to std::less<key_type>. You can, however, specify a
comparison function of your own choice. In order to do so, you pass a
function object (in the simplest case, a free standing function) as a
parameter upon construction of the map object.
In your example, map<string,int>: the default std::less<string> will be
lexicographic comparison of strings. If you know that your strings will
typically have long common initial segments but differ in length, you may
want to replace that with a short_lex order comparison predicate for
efficiency.
Hope this helps
Kai-Uwe Bux