std::multiset<Key,Compare,Allocator>::contains

来自cppreference.com
< cpp‎ | container‎ | multiset

bool contains( const Key& key ) const;
(1) (C++20 起)
template< class K > bool contains( const K& x ) const;
(2) (C++20 起)
1) 检查容器中是否有键等价于 key 的元素。
2) 检查是否有键比较等价于值 x 的元素。此重载仅若有限定标识 Compare::is_transparent 合法并指代类型才参与重载决议。它允许无需构造 Key 的实例就调用此函数。

参数

key - 要搜索的元素键值
x - 任何能通透地与键比较的类型的值

返回值

若有这种元素则为 true ,否则为 false

复杂度

与容器大小成对数。

示例

#include <iostream>
#include <set>
 
int main()
{
    std::multiset<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

参阅

寻找带有特定键的元素
(公开成员函数)
返回匹配特定键的元素数量
(公开成员函数)
返回匹配特定键的元素范围
(公开成员函数)