cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : map : lower_bound
- -
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::lower_bound public member function
      iterator lower_bound ( const key_type& x );
const_iterator lower_bound ( const key_type& x ) const;

Return iterator to lower bound

Returns an iterator pointing to the first element in the container whose key does not compare less than x (using the container's comparison object), i.e. it is either equal or greater.

Unlike upper_bound, this member function returns an iterator to the element also if it compares equal to x and not only if it compares greater.

Notice that, internally, all the elements in a map container are always ordered by their keys following the criterion defined by its comparison object, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.

Parameters

x
Key value to be compared.
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 the first element in the container whose key does not compare less than x.

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::lower_bound/upper_bound
#include <iostream>
#include <map>
using namespace std;

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

  mymap['a']=20;
  mymap['b']=40;
  mymap['c']=60;
  mymap['d']=80;
  mymap['e']=100;

  itlow=mymap.lower_bound ('b');  // itlow points to b
  itup=mymap.upper_bound ('d');   // itup points to e (not d!)

  mymap.erase(itlow,itup);        // erases [itlow,itup)

  // print content:
  for ( it=mymap.begin() ; it != mymap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}

a => 20
e => 100

Complexity

Logarithmic in size.

See also

map::upper_bound Return iterator to upper bound (public member function)
map::equal_range Get range of equal elements (public member function)
map::find Get iterator to element (public member function)
map::count Count elements with a specific key (public member function)

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