What it really checks for is contains() and not the count of the number of occurrences, right? Duplicates are not permitted either so wouldn't contains() be a better name than count()?
-
Yes, it is a bit confusing since set::count() actually does the job of `contains`. Probably for historical reasons. – taocp May 14 '13 at 04:09
-
1I think it's to match the [`multiset`](http://en.cppreference.com/w/cpp/container/multiset) interface, where there can be duplicates (and thus `count()` can return >1). Not positive though. – Xymostech May 14 '13 at 04:13
-
Blah blah consistency it is a stupid name! – Nils Jan 08 '14 at 13:05
3 Answers
It's to make it consistent with other container classes, given that one of the great aspects of polymorphism is to be able to treat different classes with the same API.
It does actually return the count. The fact that the count can only be zero or one for a set does not change that aspect.
It's not fundamentally different to a collection object that only allows two things of each "value" at the same time. In that case, it would return the count of zero, one or two, but it's still a count, the same as with a set.
The relevant part of the standard that requires this is C++11 23.2.4
which talks about the associative containers set
, multiset
, map
and multimap
. Table 102 contains the requirements for these associative containers over and above the requirements for "regular" containers, and the bit for count
is paraphrased below:
size_type a.count(k)
- returns the number of elements with key equivalent tok
. Complexity islog(a.size()) + a.count(k)
.

- 854,327
- 234
- 1,573
- 1,953
-
What does `Complexity is log(a.size()) + a.count(k)` mean? It seems recursive :) – cade Jul 31 '17 at 16:30
-
@Cade, I was worried for a bit that I'd mis-transcribed it but no. It's not *actually* recursive since there is nothing in the function calling `a.count(k)` to calculate the complexity - it's simply stating that the complexity of the function is affected by the number of elements that match the key. – paxdiablo Aug 03 '17 at 01:30
All associative containers must meet the requirements listed in §23.2.4/8 Table 102 - Associative container requirements. One of these is that they implement a.count(k)
which then
returns the number of elements with key equivalent to k
So the reason is to have a consistent interface between all associative containers. For instance, this uniformity will be very important when writing generic function templates that must work with any associative container.

- 106,671
- 19
- 240
- 328
It's a standard operation on containers that returns the number of matching elements. In things like lists, this makes perfect sense. It just so happens that on a set, there can only be one occurrence of an element and therefore count
can never return a value greater than 1.

- 13,735
- 44
- 51
-
4"In things like lists, this makes perfect sense." But std::list has no count method. – StackedCrooked May 14 '13 at 04:50
-
You're right. I was really just handwaving in my attempt to name another container type, not specifically in the STL; rather, just *any* type that could conceivably have a `count` method. @paxdiablo mentioned plenty of specific containers that *do* have a count method. – Gian May 14 '13 at 05:01