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 + +: