Is there any explanation why find() algorithm doesn't work for maps and one have to use map::find instead?
- It does work on maps, but you need to compare against a
map::value_type
(which isstd::pair<const map::key_type, map::mapped_type>
), not the key type. - Because map.find takes a key and returns a key/value pair iterator.
As noted elsewhere, it does work, but the type is the key/value pair, so you need to supply a functor/function to do the comparison. (You could probably do it with a custom operator==() overload too, though I've never tried such a thing)
However you probably do want to use the map member function find() anyway since it will give the O(logN) lookup, the algorithm std::find() is O(N).
Additional: I think you could also use std::equal_range/lower_bound/upper_bound() with a map ok, these are also O(LogN).
Do you mean equal_range? With a map, you should use the member functions lower_bound, upper_bound and equal_range. The std equivalents may provide logarithm number of comparisons, but they require linear time to walk over the elements of a container.
You should read "Effective STL" by Scott Meyers for more info on subjects like these.
"Item 43: Prefer member functions to algorithms with the same name"
For why the member function exists and why you should use it.
Scott Meyers also recommends using STL algorithms as opposed to writing your own loops (Item 43 in 2001 edition). For a simple type you should be able to use just
find(mmap.begin(), mmap.end(), "value")