STL map with "count" method

T

thomas

Hi,

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()"?
 
J

Joe Gottman

Victor said:
But std::multimap does.


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()' ...

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()

Joe Gottman
 
J

Jorgen Grahn

But std::multimap does.


Could it be for the consistency's sake? std::multimap would *not*
always return only 1 or 0, yes?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top