A STL map doesn't allow two keys having the same value.
That's to say, a "count()" method will always return 1 or 0.
So why a "count()" is needed while we better call it "exist()"?
But std::multimap does.
> That's to say, a "count()" method will always return 1 or 0.
> So why a "count()" is needed while we better call it "exist()"?
Could it be for the consistency's sake? std::multimap would *not*
always return only 1 or 0, yes?
You're free to define a stand-alone function 'exist' and implement it in
terms of 'count'. Curiously, for both std::map and std::multimap you
could either use 'count() != 0' or 'find() != end()' ...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Both would give correct results, but 'find() != end()' would be more
efficient in the multimap case. If count() equaled 100 the first
implementation would have to actually count to 100, while the second
could return true as soon as the first was found. This is the same
reason why empty() is implemented as 'begin() != end()' and not 'count()
> 0'.
Joe Gottman
It's easier to understand if you read the SGI STL documentation. A
std::map is defined as a kind of Unique Associative Container, and a
Unique Associative Container is a kind of Associative Container --
where count() makes sense in the general case.
If std::map::count() didn't exist, you wouldn't be able to write code
which used count() and say "this works on all associative containers".
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!