C++ std::equal() Function

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.
std::equal(a.begin(), a.end(), b.begin());
std::equal(a.begin(), a.end(), b.begin(), b.end());
| Call | What 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
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
| Parameter | Description |
|---|
| first1, last1 | Required. The first range. |
| first2, last2 | The second range. With only first2, the second range must be at least as long as the first. |
| pred | Optional. A function that says if two elements are equal. |
Return Value
| Return Value | Description |
|---|
true | Every pair of elements is equal. |
false | At 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.
#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
- The four-argument form first checks that both ranges have 3 elements.
- Then it checks 1 = 1, 2 = 2 and 3 = 3.
- All pairs match, so the result is
true.
Example 2 — One value differs
A single different value makes the answer false.
#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
- The first two pairs match.
- The last pair is 3 against 4, which is not equal.
- One bad pair is enough, so
equal() returns false.
Example 3 — Palindrome check
Compare the first half of a word with its reversed end.
#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
w.rbegin() walks the word backwards from the last letter.- Only the first half is compared with the reversed end. For
level that is le against le. 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.
About the author
Developer, cloud engineer, and technical writer
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