Namespaces
Variants

std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>:: contains

From cppreference.net

bool contains ( const Key & key ) const ;
(1) (自 C++23 起)
(自 C++26 起为 constexpr)
template < class K >
bool contains ( const K & x ) const ;
(2) (自 C++23 起)
(自 C++26 起为 constexpr)
1) 检查容器中是否存在键与 key 等效的元素。
2) 检查是否存在与 x 比较等价的键的元素。
此重载仅当 Compare 满足 透明性 时参与重载决议。它允许在不构造 Key 实例的情况下调用此函数。

目录

参数

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

返回值

true 表示存在这样的元素,否则为 false

复杂度

与容器大小呈对数关系。

示例

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

参见

td> 返回匹配特定键的元素数量
(公开成员函数)
查找具有特定键的元素
(公开成员函数)
返回匹配特定键的元素范围
(公开成员函数)