C++ std::equal() Function

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

Definition

In C++, std::equal() compares two ranges element by element and returns true if every pair matches.

It works across different container types, so you can compare a vector with an array. You can also pass your own rule to decide what "equal" means.

Syntax

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

C++
std::equal(a.begin(), a.end(), b.begin());
std::equal(a.begin(), a.end(), b.begin(), b.end());
CallWhat it does
std::equal(a.begin(), a.end(), b.begin());Compare a with the same number of elements from b.
std::equal(a.begin(), a.end(), b.begin(), b.end());Also checks that the lengths match (C++14).
Library declarations
C++
template<class InputIt1, class InputIt2>
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2);

// C++14: both ranges are bounded
template<class InputIt1, class InputIt2>
bool equal(InputIt1 first1, InputIt1 last1,
           InputIt2 first2, InputIt2 last2);

Parameters

ParameterDescription
first1, last1Required. The first range.
first2, last2The second range. With only first2, the second range must be at least as long as the first.
predOptional. A function that says if two elements are equal.

Return Value

Return ValueDescription
trueEvery pair of elements is equal.
falseAt least one pair differs (or the lengths differ in the four-argument form).

C++ Examples

Example 1 — Two equal vectors

Compare two vectors with the same values.

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

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

    bool same = std::equal(a.begin(), a.end(), b.begin(), b.end());
    std::cout << std::boolalpha << "Same: " << same << '\n';
    return 0;
}

How It Works

  1. The four-argument form first checks that both ranges have 3 elements.
  2. Then it checks 1 = 1, 2 = 2 and 3 = 3.
  3. All pairs match, so the result is true.

Example 2 — One value differs

A single different value makes the answer false.

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

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

    bool same = std::equal(a.begin(), a.end(), b.begin(), b.end());
    std::cout << std::boolalpha << "Same: " << same << '\n';
    return 0;
}

How It Works

  1. The first two pairs match.
  2. The last pair is 3 against 4, which is not equal.
  3. One bad pair is enough, so equal() returns false.

Example 3 — Palindrome check

Compare the first half of a word with its reversed end.

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

int main() {
    std::vector<std::string> words = {"level", "hello"};

    for (const std::string& w : words) {
        bool pal = std::equal(w.begin(), w.begin() + w.size() / 2, w.rbegin());
        std::cout << w << ": " << (pal ? "yes" : "no") << '\n';
    }
    return 0;
}

How It Works

  1. w.rbegin() walks the word backwards from the last letter.
  2. Only the first half is compared with the reversed end. For level that is le against le.
  3. hello starts with h but ends with o, so the first pair fails.

Frequently Asked Questions

In C++, std::equal() checks whether two ranges hold the same elements in the same order. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns true if the two ranges have equal elements in the same order, and false otherwise.
Yes. You can compare a std::vector with a std::list or an array, as long as the elements can be compared with ==.
For two vectors of the same type, == also checks the size and gives the same answer. std::equal() works on any ranges.

Next: equal_range()

Finds the range of elements equal to a value in a sorted range.

equal_range() 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.

24 people found this page helpful