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

-

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

Return iterator to upper bound

Returns an iterator pointing to the first element in the container which 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 it compares equal to x, but only if it compares strictly greater.

Notice that, internally, all the elements in a set container are always ordered 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 set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.

Return value

An iterator to the the first element in the container which compares greater than x.
iterator is a member type, defined in set as a bidirectional iterator type.

Example

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

int main ()
{
  set<int> myset;
  set<int>::iterator it,itlow,itup;

  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); it++)
    cout << " " << *it;
  cout << endl;

  return 0;
}

Notice that lower_bound(30) returns an iterator to 30, whereas upper_bound(60) returns an iterator to 70.

myset contains: 10 20 70 80 90

Complexity

Logarithmic in size.

See also

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

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