函数原型:
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
函数作用:
在容器中寻找值为k的元素,返回该元素的迭代器。否则,返回map.end()。
测试案例:
// map::find
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it);
// print content:
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n';
return 0;
}
elements in mymap:
a => 50
c => 150
d => 200