Use upper_bound to locate the last point : upper_bound « STL Algorithms Binary search « C++ Tutorial






#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
   const int SIZE = 10;
   int a1[] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 };
   vector< int > v( a1, a1 + SIZE );

   vector< int >::iterator upper;

   cout << "\n\nUse upper_bound to locate the last point\n"
        << "at which 7 can be inserted in order";
   upper = upper_bound( v.begin(), v.end(), 7 );
   cout << "\n   Upper bound of 7 is element " 
        << ( upper - v.begin() ) << " of vector v";

   return 0;
}








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