C++ std::equal_range() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::equal_range() returns a pair of iterators that mark the start and end of all elements equal to a value in a sorted range.

It does the work of lower_bound() and upper_bound() in one call. If the value is not found, both iterators are the same, showing where it would be inserted.

Syntax

Add #include <algorithm>, then call std::equal_range(). Start with these two lines.

C++
auto r = std::equal_range(v.begin(), v.end(), 2);
r.first, r.second
CallWhat it does
auto r = std::equal_range(v.begin(), v.end(), 2);Get both ends of the group of 2s.
r.first, r.secondThe start of the group and the position after its last element.
Library declarations
C++
template<class ForwardIt, class T>
std::pair<ForwardIt, ForwardIt>
    equal_range(ForwardIt first, ForwardIt last, const T& value);

Parameters

ParameterDescription
first, lastRequired. A sorted range.
valueRequired. The value to look for.
compOptional. The comparison that was used to sort the range.

Return Value

Return ValueDescription
std::pairfirst is the lower bound and second is the upper bound.
Empty rangeIf the value is missing, first == second.

C++ Examples

Example 1 — Find a group

Get the range of 2s and count them.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 2, 2, 3};

    auto r = std::equal_range(v.begin(), v.end(), 2);

    std::cout << "Range: [" << (r.first - v.begin()) << ", " << (r.second - v.begin()) << ")\n";
    std::cout << "Count: " << (r.second - r.first) << '\n';
    return 0;
}

How It Works

  1. The 2s sit at index 1, 2 and 3.
  2. r.first points at index 1 and r.second points at index 4, just after the group.
  3. Their distance, 3, is the count.

Example 2 — Value not found

Both iterators are the same when nothing matches.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 3, 7};

    auto r = std::equal_range(v.begin(), v.end(), 5);

    if (r.first == r.second) {
        std::cout << "Not found; insert position " << (r.first - v.begin()) << '\n';
    }
    return 0;
}

How It Works

  1. There is no 5 in the vector.
  2. An empty range means first and second are equal.
  3. They point at index 2, the place where 5 would keep the order.

Example 3 — Erase a group

Use the range to remove all matching elements.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {4, 5, 5, 6};

    auto r = std::equal_range(v.begin(), v.end(), 5);
    v.erase(r.first, r.second);

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. equal_range() finds both 5s in one search.
  2. erase() takes exactly that pair of iterators.
  3. Only 4 and 6 remain.

Frequently Asked Questions

In C++, std::equal_range() finds the range of elements equal to a value in a sorted range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns a pair of iterators: the first marks the start of the equal values and the second marks the position after the last equal value.
It gives the same result as calling lower_bound() and upper_bound() separately, but it is done in a single call.
Yes. Like all binary search algorithms, it needs a sorted range.

Next: fill()

Assigns the same value to every element in a range.

fill() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful