Get the lower bound for a sorted sequence of values with algorithm lower_bound - C++ STL Algorithm

C++ examples for STL Algorithm:lower_bound

Description

Get the lower bound for a sorted sequence of values with algorithm lower_bound

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> // ostream_iterator 
using namespace std; 

int main() /*  w  w  w .  j  a v  a2 s.  c  om*/
{ 
   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 lower-bound insertion point for 6 in v 
   vector< int >::iterator lower; 
   lower = lower_bound( v.begin(), v.end(), 6 ); 
   cout << "\n\nLower bound of 6 is element " 
       << ( lower - v.begin() ) << " of vector v"; 

   // determine lower-bound insertion point for 5 in v 
   lower = lower_bound( v.begin(), v.end(), 5 ); 
   cout << "\n      Lower bound of 5 is element " 
       << ( lower - v.begin() ) << " of vector v"; 
   cout << "\n\nUse upper_bound to locate the last point\n" 
       << "at which 7 can be inserted in order"; 

}

Result


Related Tutorials