On this page
std::ranges::mismatch, std::ranges::mismatch_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Helper types | ||
|
(3) | (since C++20) |
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
.
p
.
r
as the source range, as if using ranges::begin(r)
as first
and ranges::end(r)
as last
.
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.
Parameters
first1, last1 | - | an iterator-sentinel pair denoting the first range of the elements to compare |
r1 | - | the first range of the elements to compare |
first2, last2 | - | an iterator-sentinel pair denoting the second range of the elements to compare |
r2 | - | the second range of the elements to compare |
pred | - | predicate to apply to the projected elements |
proj1 | - | projection to apply to the first range of elements |
proj2 | - | 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
|
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
(C++20)
|
determines if two sets of elements are the same (niebloid) |
(C++20)(C++20)(C++20)
|
finds the first element satisfying specific criteria (niebloid) |
(C++20)
|
returns true if one range is lexicographically less than another(niebloid) |
(C++20)
|
searches for a range of elements (niebloid) |
finds the first position where two ranges differ (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/mismatch