当前位置:   article > 正文

c++ map.find使用方法

c++ map.find

转载至https://blog.csdn.net/derkampf/article/details/71155055

函数原型

  1. iterator find (const key_type& k);
  2. const_iterator find (const key_type& k) const;

返回值

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

例子

  1. //map::find
  2. #include <iostream>
  3. #include <map>
  4. int main ()
  5. {
  6. std::map<char,int> mymap;
  7. std::map<char,int>::iterator it;
  8. mymap['a']=50;
  9. mymap['b']=100;
  10. mymap['c']=150;
  11. mymap['d']=200;
  12. it = mymap.find('b');
  13. if (it != mymap.end())
  14. mymap.erase (it);
  15. // print content:
  16. std::cout << "elements in mymap:" << '\n';
  17. std::cout << "a => " << mymap.find('a')->second << '\n';
  18. std::cout << "c => " << mymap.find('c')->second << '\n';
  19. std::cout << "d => " << mymap.find('d')->second << '\n';
  20. std::cout << "b => " << mymap.find('b')->second << '\n';
  21. std::cout << "e => " << mymap.find('e')->second << '\n';
  22. return 0;
  23. }

执行结果

  1. Output:
  2. elements in mymap:
  3. a => 50
  4. c => 150
  5. d => 200
  6. b => 0
  7. e => 0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/992007
推荐阅读
相关标签
  

闽ICP备14008679号