STL之map::find方法的使用小例子

来源:互联网 发布:js获取android版本号 编辑:程序博客网 时间:2024/05/20 00:17

函数原型

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

返回值

An iterator to the element, if an element with specified key is found, or map::end otherwise.If the map object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.Member types iterator and const_iterator are bidirectional iterator types pointing to elements (of type value_type).Notice that value_type in map containers is an alias of pair<const key_type, mapped_type>.

例子

//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';    std::cout << "b => " << mymap.find('b')->second << '\n';    std::cout << "e => " << mymap.find('e')->second << '\n';    return 0;}

执行结果

Output:elements in mymap:a => 50c => 150d => 200b => 0e => 0
1 0
原创粉丝点击