std::map<Key,T,Compare,Allocator>::contains_C++中文网

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 的元素。此重载仅若有限定 id Compare::is_transparent 合法且代表类型才参与重载决议。它允许调用此函数而无需构造 Key 的实例。

参数

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

返回值

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

复杂度

与容器大小成对数。

示例

#include <iostream>
#include <map>
 
int main()
{  
    std::map<int,char> example = {{1,'a'},{2,'b'}};
 
    if (example.contains(2)) {
        std::cout << "Found\n";
    } else {
        std::cout << "Not found\n";
    }
}

输出:

参阅