std::equal_range is used to find the sub-range within a given range [first, last) that has all the elements equivalent to a given value. It
returns the initial and the final bound of such a sub-range.
This function requires the range to be either sorted or partitioned according to some condition such that all the elements for which the condition evaluates to true are to the left of the given value and rest all are to its right.
It can be used in two ways as shown below:
-
Comparing elements using <:
Syntax:
Template
pair
equal_range (ForwardIterator first, ForwardIterator last, const T& val);
first: Forward iterator to the first element in the range.
last: Forward iterator to the last element in the range.
val: Value of the subrange to search for in the range.
Return Value: It returns a pair object, whose member pair::first
is an iterator to the lower bound of the subrange of equivalent
values, and pair::second its upper bound.
If there is no element equivalent to val, then both first and
second points to the nearest element greater than val, or if val is
greater than any other value, then both of them point to last.
CPP
// C++ program to demonstrate the use of std::equal_range
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = { 10, 10, 30, 30, 30, 100, 10,
300, 300, 70, 70,