public member function

std::multimap::count

<map>
size_type count ( cont 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 elements having that key.

Parameters

x
Key value to be searched for.
key_type is a member type defined in multimap containers as an alias of Key, which is the first template parameter and the type of the keys for 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
17
18
19
20
21
22
23
24
25
26
27
28
29
// multimap::count
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  multimap<char,int> mymm;
  multimap<char,int>::iterator it;
  char c;

  mymm.insert(pair<char,int>('x',50));
  mymm.insert(pair<char,int>('y',100));
  mymm.insert(pair<char,int>('y',150));
  mymm.insert(pair<char,int>('y',200));
  mymm.insert(pair<char,int>('z',250));
  mymm.insert(pair<char,int>('z',300));

  for (c='x'; c<='z'; c++)
  {
    cout << "There are " << (int)mymm.count(c);
    cout << " elements with key " << c << ":";
    for (it=mymm.equal_range(c).first; it!=mymm.equal_range(c).second; ++it)
      cout << " " << (*it).second;
    cout << endl;
  }

  return 0;
}


Output:

There are 1 elements with key x: 50
There are 3 elements with key y: 100 150 200
There are 2 elements with key z: 250 300

Complexity

Logarithmic in size plus linear in the count.

See also