size_type count ( const key_type& x ) const;
Count elements with a specific key
Searches the container for an element with a key of x and returns the number of times the element appears in the container.
Parameters
- x
- Value to be searched for.
key_type is a member type defined in multiset containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.
Return value
The amount of elements in the container with a key value equivalent to x.
Member type size_type is an unsigned integral type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// multiset::count
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[]={10,73,12,22,73,73,12};
multiset<int> mymultiset (myints,myints+7);
cout << "73 appears ";
cout << (int) mymultiset.count(73);
cout << " times in mymultiset." << endl;
return 0;
}
|
Output:
73 appears 3 times in mymultiset.
|
Complexity
Logarithmic in size plus linear in the count.
See also
|