public member function
<unordered_set>

std::unordered_multiset::count

size_type count ( const key_type& k ) const;
计算具有特定键的元素
Searches the container for elements with a value of k and returns the number of elements found.

参数

k
Value of the elements to be counted.
成员类型key_typeis the type of the elements in the container. In unordered_multiset containers it is the same asvalue_type相同,定义为类模板参数的别名().

返回值

The number of elements in the container with a key equivalent to k.

成员类型size_type是一种无符号整型类型。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// unordered_multiset::count
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_multiset<std::string> myums =
    {"cow","pig","chicken","pig","pig","cow"};

  for (auto& x: {"cow","sheep","pig"}) {
    std::cout << x << ": " << myums.count(x) << std::endl;
  }

  return 0;
}

输出
cow: 2
sheep: 0
pig: 3


复杂度

平均情况:与计数元素数量成线性关系。
最坏情况:与 容器大小 成线性关系。

迭代器有效性

没有变化。

另见