Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
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
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
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
Operations on uninitialized storage
Return types
Definiert in Header <algorithm>
Aufrufsignatur
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) (seit 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) (seit C++20)
Hilfstypen
template < class I1, class I2 >
using mismatch_result = ranges:: in_in_result < I1, I2 > ;
(3) (seit C++20)

Gibt das erste nicht übereinstimmende Paar projizierter Elemente aus zwei Bereichen zurück: einer definiert durch [ first1 , last1 ) oder r1 und ein anderer definiert durch [ first2 , last2 ) oder r2 .

1) Elemente werden mit dem angegebenen binären Prädikat p verglichen.
2) Gleich wie (1) , verwendet jedoch r als Quellbereich, als ob ranges:: begin ( r ) als first und ranges:: end ( r ) als last verwendet würde.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

first1, last1 - das Iterator-Sentinel-Paar, das den ersten Bereich der zu vergleichenden Elemente definiert
r1 - der erste Bereich der zu vergleichenden Elemente
first2, last2 - das Iterator-Sentinel-Paar, das den zweiten Bereich der zu vergleichenden Elemente definiert
r2 - der zweite Bereich der zu vergleichenden Elemente
pred - Prädikat, das auf die projizierten Elemente angewendet wird
proj1 - Projektion, die auf den ersten Elementbereich angewendet wird
proj2 - Projektion, die auf den zweiten Elementbereich angewendet wird

Rückgabewert

ranges::mismatch_result mit Iteratoren zu den ersten beiden ungleichen Elementen.

Wenn beim Vergleich keine Nichtübereinstimmungen gefunden werden, bis last1 oder last2 erreicht wird, je nachdem was zuerst eintritt, enthält das Objekt den End-Iterator und den entsprechenden Iterator aus dem anderen Bereich.

Komplexität

Höchstens std:: min ( last1 - first1, last2 - first2 ) Anwendungen des Prädikats und entsprechender Projektionen.

Mögliche Implementierung

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;

Beispiel

Dieses Programm bestimmt den längsten Teilstring, der gleichzeitig am Anfang und am Ende der gegebenen Zeichenkette in umgekehrter Reihenfolge gefunden wird (möglicherweise überlappend).

#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"));
}

Ausgabe:

ab
a
ABBA
level

Siehe auch

bestimmt, ob zwei Elementgruppen identisch sind
(Algorithmus-Funktionsobjekt)
findet das erste Element, das bestimmte Kriterien erfüllt
(Algorithmus-Funktionsobjekt)
gibt true zurück, wenn ein Bereich lexikographisch kleiner als ein anderer ist
(Algorithmus-Funktionsobjekt)
sucht nach dem ersten Vorkommen eines Elementbereichs
(Algorithmus-Funktionsobjekt)
findet die erste Position, an der sich zwei Bereiche unterscheiden
(Funktionstemplate)