std::ranges::equal(3)
π std::ranges::equal(3) β bestie, are these ranges giving equal? π
okay so this is the std::ranges::equal page from the C++ Standard Libary manual, and she is lowkey iconic. we're checking whether two ranges match after projections and a predicate, no cap π
πΈ NAME πΈ
std::ranges::equal β std::ranges::equal (yes, she's the same name twice, periodt)
β¨ Synopsis β¨
Defined in header <algorithm>.
π Call signature π
(1)(since C++20)
template< std::input_iterator I1, std::sentinel_for<I1> S1,
std::input_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool
equal( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
(2)(since C++20)
template< ranges::input_range R1, ranges::input_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr bool
equal( R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
- Returns
trueif the projected values of the range[first1, last1)are equal to the projected values of the range[first2, last2), andfalseotherwise. - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
Two ranges are considered equal if they have the same number of elements and every pair of corresponding projected elements satisfies pred. That is, std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2)) returns true for all pairs of corresponding elements in both ranges.
The function-like entities described on this page are 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.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first1,last1β an iterator-sentinel pair denoting the first range of the elements to comparer1β the first range of the elements to comparefirst2,last2β an iterator-sentinel pair denoting the second range of the elements to comparer2β the second range of the elements to comparepredβ predicate to apply to the projected elementsproj1β projection to apply to the first range of elementsproj2β projection to apply to the second range of elements
π¦ Return value π¦
- If the length of the range
[first1, last1)does not equal the length of the range[first2, last2), returnsfalse. - If the elements in the two ranges are equal after projection, returns
true. - Otherwise returns
false.
π Notes π
π¨ bestie warning:ranges::equalshould not be used to compare the ranges formed by the iterators fromstd::unordered_set,std::unordered_multiset,std::unordered_map, orstd::unordered_multimapbecause 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 for equality, operator== for the corresponding container are usually preferred.
π₯ Complexity π₯
At most min(last1 - first1, last2 - first2) applications of the predicate and corresponding projections.
However, if S1 and S2 both model std::sized_sentinel_for their respective iterators, and last1 - first1 != last2 - first2 then no applications of the predicate are made (size mismatch is detected without looking at any elements).
π§ Possible implementation π§
struct equal_fn {
template<std::input_iterator I1, std::sentinel_for<I1> S1,
std::input_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool
operator()(I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
if constexpr (std::sized_sentinel_for<S1, I1> and std::sized_sentinel_for<S2, I2>)
if (std::ranges::distance(first1, last1) !=
std::ranges::distance(first2, last2))
return false;
for (; first1 != last1; ++first1, (void)++first2)
if (!std::invoke(pred, std::invoke(proj1, *first1),
std::invoke(proj2, *first2)))
return false;
return true;
}
template<ranges::input_range R1, ranges::input_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr bool
operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {},
Proj2 proj2 = {}) const
{
return (*this)(ranges::begin(r1), ranges::end(r1),
ranges::begin(r2), ranges::end(r2),
std::ref(pred), std::ref(proj1), std::ref(proj2));
} };
inline constexpr equal_fn equal;
π 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>
constexpr bool is_palindrome(const std::string_view s)
{
namespace views = std::views;
auto forward = s | views::take(s.size() / 2);
auto backward = s | views::reverse | views::take(s.size() / 2);
return std::ranges::equal(forward, backward);
}
void test(const std::string_view s)
{
std::cout << std::quoted(s) << " is "
<< (is_palindrome(s) ? "" : "not ")
<< "a palindrome\n";
}
int main()
{
test("radar");
test("hello");
static_assert(is_palindrome("ABBA") and not is_palindrome("AC/DC"));
}
π₯οΈ Output π₯οΈ
"radar" is a palindrome
"hello" is not a palindrome
π See also π
ranges::find,ranges::find_if,ranges::find_if_not(C++20) β finds the first element satisfying specific criteria (niebloid)ranges::lexicographical_compare(C++20) β returnstrueif one range is lexicographically less than another (niebloid)ranges::mismatch(C++20) β finds the first position where two ranges differ (niebloid)ranges::search(C++20) β searches for a range of elements (niebloid)ranges::equal_range(C++20) β returns range of elements matching a specific key (niebloid)equal_toβ function object implementingx == y(class template)equalβ determines if two sets of elements are the same (function template)
mwah π β from http://cppreference.com, version 2024.06.10, for std::ranges::equal(3) π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms