std::experi...es::mismatch(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

Okay bestie, this is the page for std::experi...es::mismatch from the C++ Standard Libary manual, and the full name is std::experimental::ranges::mismatch. It’s giving β€œfind the first spot where two ranges stop matching” energy, and yes, the title really has that experi...es abbreviation β€” we keep it exactly as the source has it. πŸ‘‘


✨ Synopsis ✨

Defined in header <experimental/ranges/algorithm> β€” literal path, do not touch it. 🫢

template< InputIterator I1, Sentinel<I1> S1, InputIterator I2,
Sentinel<I2> S2,

          class Proj1 = ranges::identity, class Proj2 =
ranges::identity,
          class Pred = ranges::equal_to<> >
    requires IndirectRelation<Pred, projected<I1, Proj1>,
(1) (ranges TS)
projected<I2, Proj2>>
auto mismatch( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred
= Pred{},
               Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} )

    -> ranges::tagged_pair<tag::in1(I1), tag::in2(I2)>;
template< InputRange R1, InputRange R2,

          class Proj1 = ranges::identity, class Proj2 =
ranges::identity,
          class Pred = ranges::equal_to<> >
    requires IndirectRelation<Pred,
projected<ranges::iterator_t<R1>, Proj1>,
(2) (ranges TS)
                              projected<ranges::iterator_t<R2>,
Proj2>>
auto mismatch( R1&& r1, R2&& r2, Pred pred = Pred{},
               Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} )
    -> ranges::tagged_pair<tag::in1(ranges::safe_iterator_t<R1>),

                           tag::in2(ranges::safe_iterator_t<R2>)>;
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> &&
             IndirectRelation<Pred, projected<I1, Proj1>,
(ranges TS)
                                    projected<std::decay_t<I2>,
(3) (deprecated)
Proj2>>
auto mismatch( I1 first1, S1 last1, I2&& first2_, Pred pred =
Pred{},
               Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} )

    -> ranges::tagged_pair<tag::in1(I1),
tag::in2(std::decay_t<I2>)>;
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> &&
             IndirectRelation<Pred,
projected<ranges::iterator_t<R1>, Proj1>,
(4) (ranges TS)
                                    projected<std::decay_t<I2>,
(deprecated)
Proj2>>
auto mismatch( R1&& r1, I2&& first2_, Pred pred = Pred{},
               Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} )
    -> ranges::tagged_pair<tag::in1(ranges::safe_iterator_t<Rng1>),

                           tag::in2(std::decay_t<I2>)>;
  1. Returns the first mismatching pair of elements from two ranges: one defined by [first1, last1) and another defined by [first2, last2). πŸ‘€
  2. Same as (1), but uses r1 as the first source range and r2 as the second source range, as if using ranges::begin(r1) as first1, ranges::end(r1) as last1, ranges::begin(r2) as first2, and ranges::end(r2) as last2. ✨
  3. Same as (1), but behaves as if first2 is std::decay_t<I2> first2 = std::forward<I2>(first2_); and last2 is ranges::unreachable{}. The underlying algorithm never increments first2 more than last1 - first1 times. 🫑
  4. Same as (3), but uses r1 as the first source range, as if using ranges::begin(r1) as first1 and ranges::end(r1) as last1. πŸ’…

Elements are compared using pred to the projected elements of the two ranges, as if by ranges::invoke(pred, ranges::invoke(proj1, *i), ranges::invoke(proj2, *j)). No cap, that’s the actual comparison vibe. πŸ”₯

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. ⚠️ This is the β€œdo not assume the template parameter order is stable” warning, bestie β€” keep it close.

πŸŽ€ 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 πŸ’–

A tagged_pair object with iterators to the first two non-equal elements β€” the iterator from the first range has the tag in1, and the iterator from the second range has the tag in2. 🏷️✨

If no mismatches are found when the comparison reaches last1 or last2, whichever happens first, the pair holds the end iterator and the corresponding iterator from the other range. So yeah, it can absolutely return β€œwe ran out of range” vibes, not just β€œfound a difference” vibes. πŸ’€


🌈 Complexity 🌈

At most last1 - first1 applications of the predicate and each projection. That’s the budget, bestie β€” no secret extra passes. πŸ’Έ


🧚 Possible implementation 🧚

template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
            class Proj1 = ranges::identity, class Proj2 = ranges::identity,
            class Pred = ranges::equal_to<>>
       requires IndirectRelation<Pred, projected<I1, Proj1>, projected<I2, Proj2>>
   auto mismatch(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = Pred{},
                 Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
       -> ranges::tagged_pair<tag::in1(I1), tag::in2(I2)>
   {
       while (first1 != last1 && first2 != last2 &&
              ranges::invoke(pred, ranges::invoke(proj1, *first1),
                                   ranges::invoke(proj2, *first2)))
       {
           ++first1;
           ++first2;
       }
       return {first1, first2};
   }

πŸ¦‹ Example πŸ¦‹

This section is incomplete Reason: no example

Not me showing up with no example 😭 β€” the source literally says it’s incomplete and gives β€œno example” as the reason. We preserve the emptiness, bestie. 🫢


⭐ See also ⭐

  • mismatch β€” finds the first position where two ranges differ (function template)
  • 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 β€” returns true if one range is lexicographically less than another (function template)
  • search β€” searches for a range of elements (function template)

πŸ‘‘ Category πŸ‘‘

  • * Todo no example

xoxo, your C++ bestie πŸ’– β€” source: http://cppreference.com, version/date: 2024.06.10, and the page is std::experi...es::mismatch(3). Bye bestie! πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from