cppreference.com
Create account
Namespaces
Variants
Actions
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
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 [first1last1) and [first2last2).
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:

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

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

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

finds the first position where two ranges differ
(function template)
determines if two sets of elements are the same
(algorithm function object)
finds the first element satisfying specific criteria
(algorithm function object)
compares two ranges lexicographically
(algorithm function object)
searches for the first occurrence of a range of elements
(algorithm function object)