cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : map : find
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
map
comparison operators
map::map
map::~map
member functions:
· map::begin
· map::clear
· map::count
· map::empty
· map::end
· map::equal_range
· map::erase
· map::find
· map::get_allocator
· map::insert
· map::key_comp
· map::lower_bound
· map::max_size
· map::operator=
· map::operator[]
· map::rbegin
· map::rend
· map::size
· map::swap
· map::upper_bound
· map::value_comp

-

map::find public member function
      iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;

Get iterator to element

Searches the container for an element with a value of x and returns an iterator to it if found, otherwise it returns an iterator to map::end (the element past the end of the container).

Parameters

x
Value to be searched for.
key_type is a member type defined in map containers as an alias of Key, which is the first template parameter and the type of the keys for the elements stored in the container.

Return value

An iterator to the element, if the specified key value is found, or map::end if the specified key is not found in the container.

Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.
Dereferencing this iterator accesses the element's value, which is of type pair<const Key,T>.

Example

// map::find
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<char,int> mymap;
  map<char,int>::iterator it;

  mymap['a']=50;
  mymap['b']=100;
  mymap['c']=150;
  mymap['d']=200;

  it=mymap.find('b');
  mymap.erase (it);
  mymap.erase (mymap.find('d'));

  // print content:
  cout << "elements in mymap:" << endl;
  cout << "a => " << mymap.find('a')->second << endl;
  cout << "c => " << mymap.find('c')->second << endl;

  return 0;
}

Output:

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

Complexity

Logarithmic in size.

See also

map::operator[] Access element (public member function)
map::count Count elements with a specific key (public member function)
map::lower_bound Return iterator to lower bound (public member function)
map::upper_bound Return iterator to upper bound (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us