std::experi...nges::equal(3)
π std::experi...nges::equal(3) β C++ Standard Libary bestie moment π
Okay so hi bestie!! Welcome to the C++ Standard Libary manual page for std::experi...nges::equal(3) β that's the title, that's the manual, and no we are not skipping any tea. This one is giving βranges TS loreβ and honestly it ate. π
β¨
π NAME π
std::experimental::ranges::equal - std::experimental::ranges::equal
π§Ύ Synopsis π§Ύ
Defined in header <experimental/ranges/algorithm> π«Ά
Overload (1) β (ranges TS) β¨
template< InputIterator I1, Sentinel<I1> S1, InputIterator I2,
Sentinel<I2> S2,
class Pred = ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
bool equal( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred =
Pred{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
Overload (2) β (ranges TS) πΈ
template< InputRange R1, InputRange R2, class Pred =
ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
requires IndirectlyComparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
bool equal( R1&& r1, R2&& r2, Pred pred = Pred{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
Overload (3) β (ranges TS) and (deprecated) π«
template< InputIterator I1, Sentinel<I1> S1, class I2,
class Pred = ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
requires InputIterator<std::decay_t<I2>> && !Range<I2> &&
IndirectlyComparable<I1, std::decay_t<I2>, Pred,
Proj1, Proj2>
bool equal( I1 first1, S1 last1, I2&& first2_, Pred pred = Pred{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
Overload (4) β (ranges TS) and (deprecated) π
template< InputRange R1, class I2, class Pred = ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
requires InputIterator<std::decay_t<I2>> && !Range<I2> &&
IndirectlyComparable<ranges::iterator_t<R1>,
std::decay_t<I2>, Pred, Proj1, Proj2>
bool equal( R1&& r1, I2&& first2_, Pred pred = Pred{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
So like, overload-by-overload tea:
- Returns
trueif the range[first1, last1)is equal to the range[first2, last2), andfalseotherwise. - Same as
(1), but usesr1as the first source range andr2as the second source range, as if usingranges::begin(r1)asfirst1,ranges::end(r1)aslast1,ranges::begin(r2)asfirst2, andranges::end(r2)aslast2. - Same as
(1), except that the second range is considered to end when either the first range is exhausted or the first mismatch is detected. Equivalent toreturn last1 == ranges::mismatch(first1, last1, std::forward<I2>(first2_), comp, proj1, proj2).in1(); - Same as
(3), but usesr1as the first source range, as if usingranges::begin(r1)asfirst1andranges::end(r1)aslast1.
Two ranges are considered equal if they have the same number of elements and, for every iterator i in the range [first1, last1), ranges::invoke(pred, ranges::invoke(proj1, *i), ranges::invoke(proj2, *(first2 + (i - first1)))) is true. π
Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable. π¨ Bestie, do not come for explicit template arguments unless you are ready for chaos.
π«Ά Parameters π«Ά
- first1, last1 β the first range of the elements
- r1 β the first range of the elements
- first2, last2 β the second range of the elements
- r2 β the second range of the elements
- first2_ β the beginning of the second range of the elements
- pred β predicate to apply to the projected elements
- proj1 β projection to apply to the elements in the first range
- proj2 β projection to apply to the elements in the second range
π Return value π
true if the two ranges are equal, otherwise returns false. Periodt. π
β οΈ Notes β οΈ
Ugh, important: 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. π Not the unordered containers catching strays, but facts are facts.
When comparing entire containers for equality, operator== for the corresponding container are usually preferred. π«‘
π Complexity π
1,2)IfSizedSentinel<S1, I1> && SizedSentinel<S2, I2>is satisfied andlast1 - first1 != last2 - first2, no applications of the predicate and projections. Otherwise, at mostmin(last1 - first1, last2 - first2)applications of the predicate and each projection.3,4)At mostlast1 - first1applications of the predicate and each projection.
π§ Possible implementation π§
Okay bestie, here's the implementation vibe β copy it exactly, don't freestyle it:
namespace detail
{
template<InputIterator I1, SizedSentinel<I1> S1,
InputIterator I2, SizedSentinel<I1> S2>
bool check_size(I1& first1, S1& last1, I2& first2, S2& last2)
{
return last1 - first1 != last2 - first2;
}
template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I1> S2>
bool check_size(I1& first1, S1& last1, I2& first2, S2& last2)
{
return false;
}
}
template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
class Pred = ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 = ranges::identity>
requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
bool equal(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = Pred{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
{
if (detail::check_size(first1, last1, first2, last2))
return false;
for (; first1 != last1 && first2 != last2; (void) ++first1, (void)++first2)
if (!ranges::invoke(pred, ranges::invoke(proj1, *first1),
ranges::invoke(proj2, *first2)))
return false;
return first1 == last1 && first2 == last2;
}
π Example π
This section is incomplete. Reason: no example
Not me being left on read by the example section. π
π See also π
equalβ determines if two sets of elements are the same (function template)find,find_if,find_if_notβ finds the first element satisfying specific criteria (function template)lexicographical_compareβ returnstrueif one range is lexicographically less than another (function template)mismatchβ finds the first position where two ranges differ (function template)searchβ searches for a range of elements (function template)
π·οΈ Category π·οΈ
* Todo no example
Cute sign-off time, bestie: the footer source/version is http://cppreference.com, dated 2024.06.10, and the page itself is std::experi...nges::equal(3). That's the whole footer, no cap. Mwah πβ¨
π¦ 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