Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STL map with "count" method

89 views
Skip to first unread message

thomas

unread,
Jan 30, 2009, 8:12:48 AM1/30/09
to
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()"?

Victor Bazarov

unread,
Jan 30, 2009, 9:38:01 AM1/30/09
to
thomas wrote:
> A STL map doesn't allow two keys having the same value.

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

Joe Gottman

unread,
Jan 31, 2009, 8:42:19 AM1/31/09
to
Victor Bazarov wrote:
> thomas wrote:
>> A STL map doesn't allow two keys having the same value.
>
> 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()' ...
>

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

Jorgen Grahn

unread,
Feb 3, 2009, 2:12:53 PM2/3/09
to
On Fri, 30 Jan 2009 09:38:01 -0500, Victor Bazarov <v.Aba...@comAcast.net> wrote:
> thomas wrote:
>> A STL map doesn't allow two keys having the same value.
>
> 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?

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!

0 new messages