std::ranges...atch_result(3)
π NAME π
hey bestie, welcome to the std::ranges...atch_result(3) page from the C++ Standard Libary β yes, manual name folded in right here, not copied from the header line. This page is literally the home of std::ranges::mismatch,std::ranges::mismatch_result, the ranges-y mismatch situation. β¨
πΈ Synopsis πΈ
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
>
(1) (since C++20)
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 = {}
);
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,
(2) (since C++20)
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 = {} );
π Helper types π
template< class I1, class I2 >
(3) (since C++20)
using mismatch_result = ranges::in_in_result<I1, I2>;
okay so what does it actually do? It returns the first mismatching pair of projected elements from two ranges: one defined by [first1, last1) or r1 and another defined by [first2, last2) or r2. π«Ά
- Elements are compared using the given binary predicate
p. - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
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. no cap. π
π¦ 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 π
ranges::mismatch_result with iterators to the first two non-equal elements.
If no mismatches are found when the comparison reaches last1 or last2, whichever happens first, the object holds the end iterator and the corresponding iterator from the other range. β¨
π₯ Complexity π₯
At most std::min(last1 - first1, last2 - first2) applications of the predicate and corresponding projections.
π 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 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), ranges::end(r1),
ranges::begin(r2), ranges::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).
// Run this code
#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 π
ranges::equalβ determines if two sets of elements are the same;(C++20);(niebloid)ranges::findranges::find_ifranges::find_if_notβ finds the first element satisfying specific criteria;(C++20);(niebloid)(C++20)(C++20)ranges::lexicographical_compareβ returns true if one range is lexicographically less than another;(C++20);(niebloid)ranges::searchβ searches for a range of elements;(C++20);(niebloid)mismatchβ finds the first position where two ranges differ;(function template)
mwah π source/version/date: http://cppreference.com Β· 2024.06.10 β and this was the std::ranges...atch_result(3) page from the C++ Standard Libary. periodt. π«Ά
π¦ 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