std::ranges::equal - cppreference.com

Defined in header

<algorithm>

Call signature

template<std::input_iteratorI1,std::sentinel_for<I1>S1,std::input_iteratorI2,std::sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>constexprboolequal(I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={}); (1) (since C++20)template<ranges::input_rangeR1,ranges::input_rangeR2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>constexprboolequal(R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={}); (2) (since C++20)template</*execution-policy*/Ep,std::random_access_iteratorI1,std::sized_sentinel_for<I1>S1,std::random_access_iteratorI2,std::sized_sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>boolequal(Ep&&policy,I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={}); (3) (since C++26)template</*execution-policy*/Ep,/*sized-random-access-range*/R1,/*sized-random-access-range*/R2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>boolequal(Ep&&policy,R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={}); (4) (since C++26)For the definition of /*execution-policy*/, see

this page

; for the definition of /*sized-random-access-range*/, see

this page

.

Checks whether two target ranges are equal. The elements (projected by proj1 and proj2 respectively) are compared using the binary predicate pred.

1) The target ranges are [first1, last1) and [first2, last2).

2) The target ranges are r1 and r2.

3,4) Same as (1,2), but executed according to policy.

The function-like entities described on this page are

algorithm function objects

(informally known as niebloids), that is:

Explicit template argument lists cannot be specified when calling any of them.

None of them are visible to

argument-dependent lookup

.

When any of them are found by

normal unqualified lookup

as the name to the left of the function-call operator,

argument-dependent lookup

is inhibited.

Parameters

first1, last1 - the iterator-sentinel pair defining the first target

range

r1 - the first target range first2, last2 - the iterator-sentinel pair defining the second target

range

r2 - the second target range pred - the predicate to be applied to the (projected) elements proj1 - the projection to be applied to the elements in the first target range proj2 - the projection to be applied to the elements in the second target range Return value

If the two target ranges have the same size, and each corresponding (projected) elements in the two ranges are equal, returns true. Otherwise returns false.

Complexity

Given

N1 as ranges::distance(first1,last1) or ranges::distance(r1), and

N2 as ranges::distance(first2,last2) or ranges::distance(r2):

1,2) At most min(N1,N2) applications of pred, proj1 and proj2.

3,4)𝓞(min(N1,N2)) applications of pred, proj1 and proj2.

If I1, S1, I2 and S2 pairwise model

sized_sentinel_for

(or both R1 and R2 model

sized_range

), and N1≠N2, then no comparison will be made.

Exceptions

3,4) During the execution process:

If the temporary memory resources required for parallelization are not available,

std::bad_alloc

is thrown.

If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for

standard policies

,

std::terminate

is invoked).

Notes

ranges::equal should not be used to compare the ranges formed by the iterators from

std::unordered_set

,

std::unordered_multiset

,

std::unordered_map

, or

std::unordered_multimap

because the order in which the elements are stored in those containers may be different even if the two containers store the same elements.

When comparing entire containers or string views for equality, operator== for the corresponding type are usually preferred.

ranges::equal is not guaranteed to be short-circuit. E.g. if the first pair elements of both ranges do not compare equal, the rest of elements may also be compared. Non-short-circuit comparison may happen when the ranges are compared with

std::memcmp

or implementation-specific vectorized algorithms.

Possible implementation

structequal_fn{template<std::input_iteratorI1,std::sentinel_for<I1>S1,std::input_iteratorI2,std::sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>constexprbooloperator()(I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={})const{ifconstexpr(std::sized_sentinel_for<S1,I1>&&std::sized_sentinel_for<S2,I2>)if(ranges::distance(first1,last1)!=ranges::distance(first2,last2))returnfalse;for(;first1!=last1;++first1,(void)++first2)if(!std::invoke(pred,std::invoke(proj1,*first1),std::invoke(proj2,*first2)))returnfalse;returntrue;}template<ranges::input_rangeR>constexprautoget_end(R&&r){returnranges::end(r);}template<ranges::forward_rangeR>constexprautoget_end(R&&r){returnranges::next(ranges::begin(r),ranges::end(r));}template<ranges::input_rangeR1,ranges::input_rangeR2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>constexprbooloperator()(R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={})const{return(*this)(ranges::begin(r1),get_end(r1),ranges::begin(r2),get_end(r2),std::ref(pred),std::ref(proj1),std::ref(proj2));}};inlineconstexprequal_fnequal;Example

The following code uses

ranges::equal

to test if a string is a palindrome.

Run this code

#include<algorithm>#include<iomanip>#include<iostream>#include<ranges>#include<string_view>constexprboolis_palindrome(conststd::string_views){namespaceviews=std::views;autoforward=s|views::take(s.size()/2);autobackward=s|views::reverse|views::take(s.size()/2);returnstd::ranges::equal(forward,backward);}voidtest(conststd::string_views){std::cout<<std::quoted(s)<<" is "<<(is_palindrome(s)?"":"not ")<<"a palindrome\n";}intmain(){test("radar");test("hello");static_assert(is_palindrome("ABBA")andnotis_palindrome("AC/DC"));}Output:

"radar" is a palindrome "hello" is not a palindrome See also

equal

determines if two sets of elements are the same
(function template)

[edit]

ranges::findranges::find_ifranges::find_if_not

(C++20)(C++20)(C++20)

finds the first element satisfying specific criteria
(algorithm function object)

[edit]

ranges::lexicographical_compare

(C++20)

compares two ranges lexicographically
(algorithm function object)

[edit]

ranges::mismatch

(C++20)

finds the first position where two ranges differ
(algorithm function object)

[edit]

ranges::search

(C++20)

searches for the first occurrence of a range of elements
(algorithm function object)

[edit]

ranges::equal_range

(C++20)

finds the range of elements matching the given value using binary search
(algorithm function object)

[edit]

equal_to

function object implementing x==y
(class template)

[edit]