std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::count
来自 cppreference.cn
size_type count( const Key& key ) const; |
(1) | (C++11 起) |
template< class K > size_type count( const K& x ) const; |
(2) | (C++20 起) |
1) 返回与指定参数 key 比较相等的元素的数量。
2) 返回与指定参数 x 比较等价的元素的数量。仅当 Hash::is_transparent 和 KeyEqual::is_transparent 有效且各自表示一个类型时,此重载才参与重载决议。这假定此类
Hash
可以与 K
类型和 Key
类型都可调用,并且 KeyEqual
是透明的,这共同允许在不构造 Key
实例的情况下调用此函数。目录 |
[编辑] 参数
key | - | 要计数的元素的键值 |
x | - | 可与键透明比较的任何类型的值 |
[编辑] 返回值
1) 具有键 key 的元素的数量。
2) 具有与 x 比较等价的键的元素的数量。
[编辑] 复杂度
平均而言,与具有键 key 的元素数量呈线性关系,最坏情况下与容器大小呈线性关系。
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_generic_unordered_lookup |
201811L |
(C++20) | 无序关联容器中的异构比较查找,重载 (2) |
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <unordered_map> int main() { std::unordered_multimap<int, std::string> dict = { {1, "one"}, {6, "six"}, {3, "three"} }; dict.insert({4, "four"}); dict.insert({5, "five"}); dict.insert({6, "six"}); std::cout << "dict: { "; for (auto const& [key, value] : dict) std::cout << '[' << key << "]=" << value << ' '; std::cout << "}\n\n"; for (int i{1}; i != 8; ++i) std::cout << "dict.count(" << i << ") = " << dict.count(i) << '\n'; }
可能的输出
dict: { [5]=five [4]=four [1]=one [6]=six [6]=six [3]=three } dict.count(1) = 1 dict.count(2) = 0 dict.count(3) = 1 dict.count(4) = 1 dict.count(5) = 1 dict.count(6) = 2 dict.count(7) = 0
[编辑] 参阅
查找具有特定键的元素 (公共成员函数) | |
(C++20) |
检查容器是否包含具有特定键的元素 (公共成员函数) |
返回与特定键匹配的元素范围 (公共成员函数) |