Spec-Zone.ru › C++

std::ranges::mismatch, std::ranges::mismatch_result

Defined in header <algorithm>
Call signature
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 mismatch_result<I1, I2>
    mismatch( I1 first1, S1 last1, I2 first2, S2 last2,
              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(1) (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 mismatch_result<ranges::borrowed_iterator_t<R1>,
                          ranges::borrowed_iterator_t<R2>>
    mismatch( R1&& r1, R2&& r2,
              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(2) (since C++20)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I1, std::sized_sentinel_for<I1> S1,
          std::random_access_iterator I2, std::sized_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>
ranges::mismatch_result<I1, I2>
    mismatch( Ep&& policy, I1 first1, S1 last1, I2 first2, S2 last2,
              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {});
(3) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R1,
          /*sized-random-access-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>
mismatch_result<ranges::borrowed_iterator_t<R1>,
                ranges::borrowed_iterator_t<R2>>
    mismatch( Ep&& policy, R1&& r1, R2&& r2,
              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(4) (since C++26)
Helper types
template< class I1, class I2 >
using mismatch_result = ranges::in_in_result<I1, I2>;
(5) (since C++20)

For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.

Returns a pair of iterators to the first pair of elements from two target ranges. 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

ranges::mismatch_result with iterators to the first two mismatching elements.

If the past-the-end position of either target range is reached, the return value holds the past-the-end iterator of this range and the iterator indicating the corresponding position of the other range.

Complexity

Given

  • \(\scriptsize N_1\)N1 as ranges::distance(first1, last1) or ranges::distance(r1), and
  • \(\scriptsize N_2\)N2 as ranges::distance(first2, last2) or ranges::distance(r2):
1,2) At most \(\scriptsize \min(N_1,N_2)\)min(N1,N2) applications of pred, proj1 and proj2.
3,4) \(\scriptsize \mathcal{O}(\min(N_1,N_2))\)𝓞(min(N1,N2)) applications of pred, proj1 and proj2.

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).

Possible implementation

struct mismatch_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 std::mismatch_result<I1, I2>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                   Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        for (; first1 != last1 && first2 != last2; ++first1, (void)++first2)
            if (not std::invoke(pred, std::invoke(proj1, *first1),
                                      std::invoke(proj2, *first2)))
                break;
        
        return {first1, first2};
    }
    
    template<ranges::input_range R>
    constexpr auto get_end(R&& r)
    {
        return ranges::end(r);
    }
    
    template<ranges::forward_range R>
    constexpr auto get_end(R&& r)
    {
        return ranges::next(ranges::begin(r), ranges::end(r));
    }
    
    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 ranges::mismatch_result<ranges::borrowed_iterator_t<R1>,
                                      ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r1, R2&& r2, Pred pred = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), get_end(r1),
                       ranges::begin(r2), get_end(r2),
                       std::ref(pred), std::ref(proj1), std::ref(proj2));
    }
};

inline constexpr mismatch_fn mismatch;

Example

This program determines the longest substring that is simultaneously found at the very beginning and at the very end of the given string, in reverse order (possibly overlapping).

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
 
[[nodiscard]]
constexpr std::string_view mirror_ends(const std::string_view in)
{
    const auto end = std::ranges::mismatch(in, in | std::views::reverse).in1;
    return {in.cbegin(), end};
}
 
int main()
{
    std::cout << mirror_ends("abXYZba") << '\n'
              << mirror_ends("abca") << '\n'
              << mirror_ends("ABBA") << '\n'
              << mirror_ends("level") << '\n';
    
    using namespace std::literals::string_view_literals;
    
    static_assert("123"sv == mirror_ends("123!@#321"));
    static_assert("radar"sv == mirror_ends("radar"));
}

Output:

ab
a
ABBA
level

See also

mismatch
finds the first position where two ranges differ
(function template)
ranges::equal
(C++20)
determines if two sets of elements are the same
(algorithm function object)
ranges::findranges::find_ifranges::find_if_not
(C++20)(C++20)(C++20)
finds the first element satisfying specific criteria
(algorithm function object)
ranges::lexicographical_compare
(C++20)
compares two ranges lexicographically
(algorithm function object)
ranges::search
(C++20)
searches for the first occurrence of a range of elements
(algorithm function object)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/algorithm/ranges/mismatch

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API