std::
set::count
公众成员函数 <set>
size_type count (const value_type& val) const;
计数具有特定值的元素
在容器中搜索与val相等的元素并返回匹配的数量。
因为set容器中的所有元素都是唯一的,所以函数只能返回1(如果找到元素)或0(否则)。
如果容器的比较对象返回false(即,无论元素作为参数以何种顺序传递),
则认为set中的两个元素是等效的。
☲ 参数
-
val
-
要搜索的值。
成员类型value_type是容器中元素的类型,在set中定义为它的第一个模板形参(T)的别名。
☉ 返回值
如果容器包含与val等价的元素,则为1;否则为0。
成员类型size_type是一个unsigned整型。
☣ 示例
// set::count
#include <iostream>
#include <set>
int main ()
{
std::set<int> myset;
// set some initial values:
for (int i=1; i<5; ++i) myset.insert(i*3); // set: 3 6 9 12
for (int i=0; i<10; ++i)
{
std::cout << i;
if (myset.count(i)!=0)
std::cout << " is an element of myset.\n";
else
std::cout << " is not an element of myset.\n";
}
return 0;
} |
输出:
0 is not an element of myset.
1 is not an element of myset.
2 is not an element of myset.
3 is an element of myset.
4 is not an element of myset.
5 is not an element of myset.
6 is an element of myset.
7 is not an element of myset.
8 is not an element of myset.
9 is an element of myset.
✥ 复杂度
集合大小呈对数.
☣ 迭代器的有效性
不变
⇄ 数据竞争
容器被访问。
并发地访问集合的元素是安全的。
☂ 异常安全性
强保证:如果抛出异常,容器中不会有任何变化。
🍄 另请参阅
set::find |
获取元素迭代器(公众成员函数) |
set::size |
返回大小(公众成员函数) |
set::equal_range |
获取相等元素的范围(公众成员函数) |