Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

equal_range


Algorithm

Summary

Find the largest subrange in a collection into which a given value can be inserted without violating the ordering of the collection.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

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

Description

The equal_range algorithm performs a binary search on an ordered container to determine where the element value can be inserted without violating the container's ordering. The library includes two versions of the algorithm. The first version uses the less than operator (operator <) to search for the valid insertion range, and assumes that the sequence was sorted using the less than operator. The second version allows you to specify a function object of type Compare, and assumes that Compare was the function used to sort the sequence. The function object must be a binary predicate.

equal_range returns a pair of iterators, i and j, that define a range containing elements equivalent to value, in other words, the first and last valid insertion points for value. If value is not an element in the container, i and j are equal. Otherwise, i points to the first element not "less" than value, and j points to the first element greater than value. In the second version, "less" is defined by the comparison object. Formally, equal_range returns a sub-range [i, j) such that value can be inserted at any iterator k within the range. Depending upon the version of the algorithm used, k must satisfy one of the following conditions:

!(*k < value) && !(value < *k)

or

comp(*k,value) == false && comp(value, *k) == false

The range [first,last) is assumed to be sorted. Type T must be LessThanComparable.

Complexity

equal_range performs at most 2 * log(last - first) + 1 comparisons.

Example

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:

vector<int,allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

binary_function, lower_bound, upper_bound



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998