std::
map::count
公众成员函数 <map>
size_type count (const value_type& val) const;
计数具有特定键的元素
在容器中搜索键值等于
k的元素,并返回匹配的数量。
因为
map容器中的所有元素都是唯一的,所以函数只能返回1(如果找到元素)或0。
如果容器的比较对象返回
false(即,无论键作为参数以何种顺序传递),
则认为
map中的两个键是等效的。
☲ 参数
-
K
-
要搜索的键。
成员类型key_type是容器中元素键的类型,在map中定义为其第一个模板参数(Key)的别名。
☉ 返回值
如果容器中包含键值等于
k的元素,则为1,否则为0。
成员类型
size_type是一个
unsigned整型。
☣ 示例
// map::count
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
char c;
mymap ['a']=101;
mymap ['c']=202;
mymap ['f']=303;
for (c='a'; c<'h'; c++)
{
std::cout << c;
if (mymap.count(c)>0)
std::cout << " is an element of mymap.\n";
else
std::cout << " is not an element of mymap.\n";
}
return 0;
}
|
输出:
a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is not an element of mymap.
e is not an element of mymap.
f is an element of mymap.
g is not an element of mymap.
✥ 复杂度
大小对数.
☣ 迭代器的有效性
不变
⇄ 数据竞争
容器被访问。
不访问映射值:并发访问或修改元素是安全的。
☂ 异常安全性
强保证:如果抛出异常,容器中不会有任何变化。
🍄 另请参阅
map::find |
获取元素迭代器(公众成员函数) |
map::size |
返回大小(公众成员函数) |
map::equal_range |
获取相等元素的范围(公众成员函数) |