C++ examples for STL Algorithm:upper_bound
Get upper bound for a sorted sequence of values with algorithm upper_bound
#include <iostream> #include <algorithm> // algorithm definitions #include <vector> // vector class-template definition #include <iterator> // ostream_iterator using namespace std; int main() /*from ww w. j a v a 2s . com*/ { const int SIZE = 10; int a1[ SIZE ] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 }; vector< int > v( a1, a1 + SIZE ); // copy of a1 ostream_iterator< int > output( cout, " " ); cout << "Vector v contains:\n" ; copy( v.begin(), v.end(), output ); // determine upper-bound insertion point for 6 in v vector< int >::iterator upper; upper = upper_bound( v.begin(), v.end(), 6 ); cout << "\nUpper bound of 6 is element " << ( upper - v.begin() ) << " of vector v"; // determine upper-bound insertion point for 7 in v upper = upper_bound( v.begin(), v.end(), 7 ); cout << "\n Upper bound of 7 is element " << ( upper - v.begin() ) << " of vector v"; cout << "\n\nUse equal_range to locate the first and\n" << "last point at which 5 can be inserted in order"; }