Use equal_range to determine both the lower- and upper-bound insertion points : equal_range « STL Algorithms Binary search « C++






Use equal_range to determine both the lower- and upper-bound insertion points

  
 

#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 );

   std::pair< std::vector< int >::iterator, std::vector< int >::iterator > eq;
   eq = std::equal_range( v.begin(), v.end(), 6 );
   cout << "\n\n\nLower bound of 6 is element " << ( eq.first - v.begin() ) << " of vector v \n\n\n";
   cout << "Upper bound of 6 is element " << ( eq.second - v.begin() ) << " of vector v";

   return 0;
}

/* 
2 2 4 4 4 6 6 6 6 8


Lower bound of 6 is element 5 of vector v


Upper bound of 6 is element 9 of vector v
 */        
    
  








Related examples in the same category

1.equal_range and distance
2.Use equal function to check whether both collections are equal
3.Use equal function to check for corresponding even and odd elements
4.Get equal range with equal_range