std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: contains
From cppreference.net
<
cpp
|
container
|
unordered map
C++
Containers library
|
(C++17)
|
||||
| Sequence | ||||
|
(C++11)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
|
(C++11)
|
||||
| Associative | ||||
| Unordered associative | ||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
| Adaptors | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Views | ||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::unordered_map
| Member types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Deduction guides (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
bool
contains
(
const
Key
&
key
)
const
;
|
(1) |
(自 C++20 起)
(自 C++26 起为 constexpr) |
|
template
<
class
K
>
bool contains ( const K & x ) const ; |
(2) |
(自 C++20 起)
(自 C++26 起为 constexpr) |
1)
检查容器中是否存在键等价于
key
的元素。
2)
检查是否存在与
x
比较等价的键的元素。
此重载仅当
Hash
与
KeyEqual
均为
透明函数对象
时参与重载决议。该条件要求
Hash
可同时被
K
和
Key
类型调用,且
KeyEqual
具有透明性,二者结合使得无需构造
Key
实例即可调用此函数。
目录 |
参数
| key | - | 要搜索元素的键值 |
| x | - | 可与键进行透明比较的任意类型值 |
返回值
true 表示存在这样的元素,否则为 false 。
复杂度
平均情况下为常数时间复杂度,最坏情况下与容器大小呈线性关系。
示例
运行此代码
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, char> example{{1, 'a'}, {2, 'b'}}; for (int x : {2, 5}) if (example.contains(x)) std::cout << x << ": Found\n"; else std::cout << x << ": Not found\n"; }
输出:
2: Found 5: Not found
参见
|
查找具有特定键的元素
(公开成员函数) |
|
|
返回匹配特定键的元素数量
(公开成员函数) |
|
|
返回匹配特定键的元素范围
(公开成员函数) |