C++ Unordered_multimap 库 - find() 函数
描述
C++ 函数 std::unordered_multimap::find() 查找与键 k 关联的元素。
如果操作成功,则方法返回指向元素的迭代器,否则返回指向 unordered_map::end() 的迭代器。
声明
以下是 std::unordered_multimap::find() 函数形式 std::unordered_map() 头的声明。
C++11
iterator find (const key_type& k); const_iterator find (const key_type& k) const;
参数
k − 要搜索的关键词。
返回值
如果 object 是常量限定的,则方法返回一个常量迭代器,否则返回非常量迭代器。
时间复杂度
常数,即平均情况下的 O(1)。
线性,即最坏情况下的 O(n)。
示例
以下示例显示了 std::unordered_multimap::find() 函数的用法。
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> umm = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5}
};
auto it = umm.find('b');
cout << "Iterator point to " << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Iterator point to b = 2

