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.

迭代器有效性

没有变化。

数据竞争

访问容器。
不访问映射值:并发访问或修改元素是安全的。

异常安全

强保证:如果抛出异常,容器没有发生变化。

另见