cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : multimap : upper_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
multimap
comparison operators
multimap::multimap
multimap::~multimap
member functions:
· multimap::begin
· multimap::clear
· multimap::count
· multimap::empty
· multimap::end
· multimap::equal_range
· multimap::erase
· multimap::find
· multimap::get_allocator
· multimap::insert
· multimap::key_comp
· multimap::lower_bound
· multimap::max_size
· multimap::operator=
· multimap::rbegin
· multimap::rend
· multimap::size
· multimap::swap
· multimap::upper_bound
· multimap::value_comp

-

multimap::upper_bound public member function
      iterator upper_bound ( const key_type& x );
const_iterator upper_bound ( const key_type& x ) const;

Return iterator to upper bound

Returns an iterator pointing to the first element in the container whose key compares greater than x (using the container's comparison object).

Unlike lower_bound, this member function does not return an iterator to the element if its key compares equal to x, but only if it compares strictly greater.

Notice that, internally, all the elements in a multimap 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 also compare greater than x.

Parameters

x
Key value to be compared.
key_type is a member type defined in multimap 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 compares greater than x.

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

Example

// multimap::lower_bound/upper_bound
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  multimap<char,int> mymultimap;
  multimap<char,int>::iterator it,itlow,itup;

  mymultimap.insert(pair<char,int>('a',10));
  mymultimap.insert(pair<char,int>('b',121));
  mymultimap.insert(pair<char,int>('c',1001));
  mymultimap.insert(pair<char,int>('c',2002));
  mymultimap.insert(pair<char,int>('d',11011));
  mymultimap.insert(pair<char,int>('e',44));

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

  // print range [itlow,itup):
  for ( it=itlow ; it != itup; it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}

b => 121
c => 1001
c => 2002
d => 11011

Complexity

Logarithmic in size.

See also

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

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