Skip to main content

std::equal() algorithm

// (1)
template< class InputIt1, class InputIt2 >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 );

// (8)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
BinaryPredicate p );
  • (1 - 2) Returns true if the range [first1; last1) is equal to the range [first2; first2 + (last1 - first1)), and false otherwise.

  • (3 - 4) Returns true if the range [first1; last1) is equal to the range [first2; last2), and false otherwise.

  • (5 - 8) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (since C++20) is true.

Two ranges are considered equal if they have the same number of elements and, for every iterator i in the range [first1; last1), *i equals *(first2 + (i - first1)).

The overloads (1, 3, 5, 7) use operator== to determine if two elements are equal, whereas overloads (2, 4, 6, 8) use the given binary predicate p.

Parameters

first1
last1

The first range of elements compare.

first2
last2

The second range of elements compare.

policy

The execution policy to use. See execution policy for details.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11))
  • The types Type1 and Type2 must be such that an object of type InputIt1 and InputIt2 can be dereferenced and then implicitly converted to them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator

Return value

If the elements in the two ranges are equal, returns true.
Otherwise, returns false.

Complexity

  • (1 - 2) At most last1 - first1 applications of the predicate.

  • (3 - 4) At most min(last1 - first1, last2 - first2) applications of the predicate. However, if InputIt1 and InputIt2 meet the requirements of LegacyRandomAccessIterator and last1 - first1 != last2 - first2, then no applications of the predicate are made (size mismatch is detected without looking at any elements).

  • (5 - 8) Same, but the complexity is specified as O(x), rather than "At most x".

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

equal (1)
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
for (; first1 != last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;

return true;
}
equal (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p)
{
for (; first1 != last1; ++first1, ++first2)
if (!p(*first1, *first2))
return false;

return true;
}

Examples

Main.cpp
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string_view>

constexpr bool is_palindrome(const std::string_view& s)
{
return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin());
}

void test(const std::string_view& s)
{
std::cout << std::quoted(s)
<< (is_palindrome(s) ? " is" : " is not")
<< " a palindrome\n";
}

int main()
{
test("radar");
test("hello");
}
Output
"radar" is a palindrome
"hello" is not a palindrome
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::equal() algorithm

// (1)
template< class InputIt1, class InputIt2 >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 );

// (2)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p );

// (3)
template< class InputIt1, class InputIt2 >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );

// (4)
template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p );

// (7)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 );

// (8)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
BinaryPredicate p );
  • (1 - 2) Returns true if the range [first1; last1) is equal to the range [first2; first2 + (last1 - first1)), and false otherwise.

  • (3 - 4) Returns true if the range [first1; last1) is equal to the range [first2; last2), and false otherwise.

  • (5 - 8) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (since C++20) is true.

Two ranges are considered equal if they have the same number of elements and, for every iterator i in the range [first1; last1), *i equals *(first2 + (i - first1)).

The overloads (1, 3, 5, 7) use operator== to determine if two elements are equal, whereas overloads (2, 4, 6, 8) use the given binary predicate p.

Parameters

first1
last1

The first range of elements compare.

first2
last2

The second range of elements compare.

policy

The execution policy to use. See execution policy for details.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11))
  • The types Type1 and Type2 must be such that an object of type InputIt1 and InputIt2 can be dereferenced and then implicitly converted to them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator

Return value

If the elements in the two ranges are equal, returns true.
Otherwise, returns false.

Complexity

  • (1 - 2) At most last1 - first1 applications of the predicate.

  • (3 - 4) At most min(last1 - first1, last2 - first2) applications of the predicate. However, if InputIt1 and InputIt2 meet the requirements of LegacyRandomAccessIterator and last1 - first1 != last2 - first2, then no applications of the predicate are made (size mismatch is detected without looking at any elements).

  • (5 - 8) Same, but the complexity is specified as O(x), rather than "At most x".

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

equal (1)
template<class InputIt1, class InputIt2>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
for (; first1 != last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;

return true;
}
equal (2)
template<class InputIt1, class InputIt2, class BinaryPredicate>
constexpr //< since C++20
bool equal(InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPredicate p)
{
for (; first1 != last1; ++first1, ++first2)
if (!p(*first1, *first2))
return false;

return true;
}

Examples

Main.cpp
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string_view>

constexpr bool is_palindrome(const std::string_view& s)
{
return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin());
}

void test(const std::string_view& s)
{
std::cout << std::quoted(s)
<< (is_palindrome(s) ? " is" : " is not")
<< " a palindrome\n";
}

int main()
{
test("radar");
test("hello");
}
Output
"radar" is a palindrome
"hello" is not a palindrome
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.