命名空间
变体
操作

std::unordered_set<Key,Hash,KeyEqual,Allocator>::contains

来自 cppreference.cn
 
 
 
 
bool contains( const Key& key ) const;
(1) (since C++20)
template< class K >
bool contains( const K& x ) const;
(2) (since C++20)
1) 检查容器中是否存在键等价于 key 的元素。
2) 检查容器中是否存在键与值 x 等价 的元素。仅当 Hash::is_transparentKeyEqual::is_transparent 有效且各自表示类型时,此重载才参与重载解析。这假定此类 Hash 可使用 KKey 类型调用,并且 KeyEqual 是透明的,这共同允许在不构造 Key 实例的情况下调用此函数。

目录

[编辑] 参数

key - 要搜索的元素的键值
x - 任何可以与键透明比较的类型的值

[编辑] 返回值

true 如果存在这样的元素,否则为 false

[编辑] 复杂度

平均为常数时间,最坏情况为容器大小的线性时间。

[编辑] 示例

#include <iostream>
#include <unordered_set>
 
int main()
{
    std::unordered_set<int> example{1, 2, 3, 4};
 
    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

[编辑] 参见

查找具有特定键的元素
(公共成员函数) [编辑]
返回匹配特定键的元素数量
(公共成员函数) [编辑]
返回匹配特定键的元素范围
(公共成员函数) [编辑]