equal_range


Return the lower and upper bounds within a range.

Search a pair of iterators equal to the lower bound and upper bound for value . The first version assumes that the elements in the range are already sorted using operator< . The second version assumes that the elements in the range are already sorted using compare . For information about lower and upper bounds, consult the algorithm catalog entries for lower_bound() and upper_bound() .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class ForwardIterator, class T >
pair< ForwardIterator, ForwardIterator > equal_range
  (
  ForwardIterator first,
  ForwardIterator last,
  const T& value
  );

template< class ForwardIterator, class T, class Compare >
pair< ForwardIterator, ForwardIterator > equal_range
  (
  ForwardIterator first,
  ForwardIterator last,
  const T& value,
  Compare compare
  );
  

Complexity

Time complexity is O(log(N)) for random access iterators, O(N) for all other iterators. Space complexity is constant.

Example <ospace/osstd/examples/eqlrnge0.cpp>
#include <iostream>
#include <algorithm>
#include <utility>

int numbers[ 10 ] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3 };

void
main()
  {
  pair< int*, int* > range;

  range = equal_range( numbers, numbers + 10, 2 );

  cout
    << "2 can be inserted from before index "
    << ( range.first - numbers )
    << " to before index "
    << ( range.second - numbers )
    << "\n";
  }

2 can be inserted from before index 4 to before index 8
Example <ospace/osstd/examples/eqlrng1.cpp>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <utility>
#include <vector>

void
main()
  {
  vector< int > v ( 10 );
  for ( size_t i = 0; i < v.size(); ++i )
    v[ i ] = i / 3;
  ostream_iterator< int > iter( cout, " " );
  cout << "Within the collection:\n\t";
  copy( v.begin(), v.end(), iter );
  pair< vector< int >::iterator, vector< int >::iterator > range;

  range = equal_range( v.begin(), v.end(), 2 );

  cout
    << "\n2 can be inserted from before index "
    << ( range.first - v.begin() )
    << " to before index "
    << ( range.second - v.begin() )
    << "\n";
  }

Within the collection:
        0 0 0 1 1 1 2 2 2 3
2 can be inserted from before index 6 to before index 9
Example <ospace/osstd/examples/eqlrng2.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#include <iterator>
#include <utility>

char chars[] = "aabbccddggghhklllmqqqqssyyzz";

void
main()
  {
  const unsigned count = sizeof(chars ) - 1;
  ostream_iterator< char > iter( cout );
  cout << "Within the collection:\n\t";
  copy(chars, chars + count, iter );
  pair< char*, char* > range;

  range = equal_range
    (
    chars,
    chars + count,
    `q',
    less< char >()
    );

  cout
    << "\nq can be inserted from before index "
    << ( range.first - chars )
    << " to before index "
    << ( range.second - chars )
    << "\n";
  }

Within the collection
        aabbccddggghhklllmqqqqssyyzz
q can be inserted from before index 18 to before index 22

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.