Use equal_range to determine both the lower- and upper-bound insertion points : equal_range « 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 );

   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








26.2.equal_range
26.2.1.Use equal_range to determine both the lower- and upper-bound insertion points
26.2.2.equal_range and distance
26.2.3.Use equal function to check whether both collections are equal
26.2.4.Use equal function to check for corresponding even and odd elements
26.2.5.Use equal_range to locate the first and last point at which 5 can be inserted in order
26.2.6.equal_range( v.begin(), v.end(), 6 )