C++ 映射库 - find() 函数



描述

C++ 函数 std::map::find() 查找与键关联的元素k.

如果操作成功,则方法返回指向该元素的迭代器,否则返回指向map::end().

声明

以下是来自 std::map 头文件的 std::map::find() 函数的声明。

C++98

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

k - 要搜索的键。

返回值

如果对象被限定为常量,则方法返回一个常量迭代器,否则返回非常量迭代器。

异常

此成员函数不抛出任何异常。

时间复杂度

对数,即 O(log n)

示例

以下示例显示了 std::map::find() 函数的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   auto it = m.find('c');

   cout << "Iterator points to " << it->first << 
      " = " << it->second << endl;

   return 0;
}

让我们编译并运行上述程序,这将产生以下结果:

Iterator points to c = 3
map.htm
广告