C++ Reference ยท Algorithms
The std::equal and std::mismatch algorithms
Reference for C++ std::equal and std::mismatch: compare two ranges element by element, find the first difference, use predicates and avoid length bugs.
std::equal answers whether two ranges hold equal elements in the same order. std::mismatch goes one step further: it returns the positions of the first pair of elements that differ, as a pair of iterators, so you can report where two sequences diverge.
Both compare element by element with == (or a predicate you supply). The classic palindrome check, comparing a string with its reverse, is a one-line std::equal. Always use the four-iterator overloads (or the ranges versions) when the lengths might differ.
bool same = std::equal(a.begin(), a.end(), b.begin(), b.end()); auto [pa, pb] = std::mismatch(a.begin(), a.end(), b.begin(), b.end());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class InIt1, class InIt2> bool equal(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2); bool equal(InIt1 first1, InIt1 last1, InIt2 first2); // unsafe: length of range 2 is assumed // each also with a trailing BinaryPredicate pred template<class InIt1, class InIt2> std::pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2); // and a three-iterator form, plus predicate overloads
| Name | Description |
|---|---|
first1, last1 |
The first range. |
first2, last2 |
The second range (the three-argument forms give only first2). |
pred |
Binary predicate that returns true when two elements count as equal. |
Returns: equal returns true if the ranges have the same length and every pair is equal. mismatch returns a pair of iterators to the first differing elements; if one range ends first, that iterator is its last, and if no difference exists both point to the end of the compared part.
Errors / undefined behavior: the three-iterator overloads assume the second range is at least as long as the first. A shorter second range makes them read out of bounds, which is undefined behavior.
Usage notes
- Use the four-iterator form. The three-iterator
equal(first1, last1, first2)is a leftover from C++98 and is dangerous when the second container is shorter. - Complexity: at most min(N1, N2) comparisons; for random-access ranges with the four-iterator form, a length mismatch is detected immediately.
- Not a lexicographic comparison:
equalandmismatchonly say same or different. For "which one is smaller" uselexicographical_compare. - Containers: most standard containers already provide
==, which does the same for whole containers.equalis for sub-ranges and for mixed container types. - Floating point: comparing floats with
==throughequalfails for values that differ by rounding. Pass an epsilon predicate. - Prefix and suffix checks:
std::equalwith a prefix range andstd::mismatchfor the common prefix are handy;std::string::starts_with(C++20) is shorter for strings. - Ranges:
std::ranges::equalandstd::ranges::mismatch(C++20) take whole ranges, projections and always check the lengths.