Simple use of iterators in map in C + +:

Source: Internet
Author: User

Public member Function<map>Std::map::find
      Iterator Find (const key_type& k); const_iterator find (const key_type& k) const;
Get iterator to Element

Searches the container for an element with a key equivalent to K and returns an iterator to it if found, Otherwise it returns an iterator to Map::end.

Both keys are considered equivalent if the container ' s comparison object returns false reflexively (i.e., No matter the order in which the elements is passed as arguments).

Another member function, Map::count, can be used to just check whether a particular key exists.

Parameters
K
Key to is searched for.
Member type key_type is the type of the keys for the elements in the container, defined in map as an alias of Its first template parameter ( Key).

Return value

An iterator to the element, if a 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 is bidirectional iterator types pointing to elements (of type value_type).
Notice that value_type in map containers are an alias of Pair<const Key_type, mapped_type>.

Example
1
2
3
4
5
6
7
8
9


15


+
(
)
,
,
,
,
,
,
  //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;}  
Edit & Run



Output:

elements in mymap:a => 50c => 150d => 200

Simple use of iterators in map in C + +:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.