描述
map::find()是C++ STL中的内置函数,该函数返回一个迭代器或常量迭代器,该迭代器或常量迭代器引用键在映射中的位置。如果键不存在于Map容器中,则它返回引用map.end()的迭代器或常量迭代器。
定义
iterator map_name.find(key)
constant iterator map_name.find(key)
说明
参数:该函数接受一个强制性参数键,该键指定要在Map容器中搜索的键。
·
返回值:该函数返回一个迭代器或常量迭代器,该迭代器或常量迭代器引用键在映射中的位置。如果映射容器中不存在该键,则它返回引用map.end()的迭代器或常量迭代器。
举例
// C++ program for illustration
// of map::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 20 });
mp.insert({ 4, 50 });
cout << "The elements from position 3 in map are : \n";
cout << "KEY\tELEMENT\n";
// find() function finds the position at which 3 is
for (auto itr = mp.find(3); itr != mp.end(); itr++)
cout << itr->first
<< '\t' << itr->second << '\n';
return 0;
}
输出结果
The elements from position 3 in map are :
KEY ELEMENT
3 20
4 50
详细介绍参考:
https://vimsky.com/examples/usage/map-find-function-in-c-stl.html