命名空间
变体
操作

std::map<Key,T,Compare,Allocator>::contains

来自 cppreference.cn
< cpp‎ | container‎ | map
 
 
 
 
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 等价 的元素。仅当限定标识符 Compare::is_transparent 有效并表示类型时,此重载才参与重载决议。它允许在不构造 Key 实例的情况下调用此函数。

目录

[edit] 参数

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

[edit] 返回值

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

[edit] 复杂度

容器大小的对数。

[edit] 示例

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

[edit] 参见

查找具有特定键的元素
(public member function) [edit]
返回匹配特定键的元素数量
(public member function) [edit]
返回匹配特定键的元素范围
(public member function) [edit]