public member function
<map>

std::multimap::count

size_type count (const key_type& k) const;
計算具有特定鍵的元素
Searches the container for elements with a key equivalent to k and returns the number of matches.

Two keys are considered equivalent if the container's comparison object returnsfalsereflexively (i.e., no matter the order in which the keys are passed as arguments).

引數

k
要搜尋的鍵。
成員型別key_typeis the type of the element keys in the container, defined in map as an alias of its first template parameter ().

返回值

The number of elements in the container contains that have a key equivalent to k.

成員型別size_type是一種無符號整型型別。

示例

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
// multimap::count
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymm;

  mymm.insert(std::make_pair('x',50));
  mymm.insert(std::make_pair('y',100));
  mymm.insert(std::make_pair('y',150));
  mymm.insert(std::make_pair('y',200));
  mymm.insert(std::make_pair('z',250));
  mymm.insert(std::make_pair('z',300));

  for (char c='x'; c<='z'; c++)
  {
    std::cout << "There are " << mymm.count(c) << " elements with key " << c << ":";
    std::multimap<char,int>::iterator it;
    for (it=mymm.equal_range(c).first; it!=mymm.equal_range(c).second; ++it)
      std::cout << ' ' << (*it).second;
    std::cout << '\n';
  }

  return 0;
}

輸出

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


複雜度

Logarithmic in size, plus linear in the number of matches.

迭代器有效性

沒有變化。

資料競爭

訪問容器。
不訪問對映值:併發訪問或修改元素是安全的。

異常安全

強保證:如果丟擲異常,容器沒有發生變化。

另見