Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

unordered_set::count

公众成员函数  <unordered_set>
size_type count ( const key_type& k ) const;

计数具有特定值的元素

在容器中搜索值为k的元素,并返回所找到的元素数量。因为unordered_set容器不允许重复值, 这意味着如果容器中存在具有该值的元素,则该函数实际返回1,否则返回0

☲  参数


K
要计数的元素的值。
成员类型key_type是容器中元素的类型。在unordered_set容器中, 它与value_type相同,定义为类的第一个模板形参(Key)的别名。

☉  返回值



如果找到值等于k的元素,则为1,否则为0
成员类型size_type是一个unsigned整型。

☣  示例



// unordered_set::count
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset = { "hat", "umbrella", "suit" };

  for (auto& x: {"hat","sunglasses","suit","t-shirt"}) {
    if (myset.count(x)>0)
      std::cout << "myset has " << x << std::endl;
    else
      std::cout << "myset has no " << x << std::endl;
  }

  return 0;
}

输出:
myset has hat
myset has no sunglasses
myset has suit
myset has no t-shirt

✥ 复杂度



平均情况下:常数。
最坏情况:容器大小呈线性。

☣ 迭代器的有效性



不变

🍄  另请参阅



unordered_set::find 获取元素迭代器(公众成员函数)
unordered_set::size 返回容器大小(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图