Return iterator from std::upper_bound : upper_bound « STL Algorithms Binary search « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a1[ 10 ] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 };
   std::vector< int > v( a1, a1 + 10 );
   std::ostream_iterator< int > output( cout, " " );

   std::copy( v.begin(), v.end(), output );

   // determine upper-bound insertion point for 6 in v
   std::vector< int >::iterator upper;
   upper = std::upper_bound( v.begin(), v.end(), 6 );
   cout << "::"<< ( upper - v.begin() );

   return 0;
}
2 2 4 4 4 6 6 6 6 8 ::9








26.4.upper_bound
26.4.1.Return iterator from std::upper_bound
26.4.2.lower_bound and upper_bound
26.4.3.Use upper_bound to locate the last point
26.4.4.Upper bound of 6