Namespaces
Variants

std::ranges:: adjacent_find

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:: forward_iterator I, std:: sentinel_for < I > S, class Proj = std:: identity ,

std:: indirect_binary_predicate <
std :: projected < I, Proj > ,
std :: projected < I, Proj >> Pred = ranges:: equal_to >
constexpr I

adjacent_find ( I first, S last, Pred pred = { } , Proj proj = { } ) ;
(1) (seit C++20)
template < ranges:: forward_range R, class Proj = std:: identity ,

std:: indirect_binary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj > ,
std :: projected < ranges:: iterator_t < R > , Proj >> Pred = ranges:: equal_to >
constexpr ranges:: borrowed_iterator_t < R >

adjacent_find ( R && r, Pred pred = { } , Proj proj = { } ) ;
(2) (seit C++20)

Durchsucht den Bereich [ first , last ) nach den ersten zwei aufeinanderfolgenden gleichen Elementen.

1) Elemente werden mit pred verglichen (nach der Projektion mit der Projektion proj ).
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

first, last - das Iterator-Sentinel-Paar, das den Bereich der zu untersuchenden Elemente definiert
r - der Bereich der zu untersuchenden Elemente
pred - Prädikat, das auf die projizierten Elemente angewendet wird
proj - Projektion, die auf die Elemente angewendet wird

Rückgabewert

Ein Iterator zum ersten des ersten Paars identischer Elemente, also der erste Iterator it für den bool ( std:: invoke ( pred, std:: invoke ( proj1, * it ) , std:: invoke ( proj, * ( it + 1 ) ) ) ) gleich true ist.

Falls keine solchen Elemente gefunden werden, wird ein Iterator gleich last zurückgegeben.

Komplexität

Genau min ( ( result - first ) + 1 , ( last - first ) - 1 ) Anwendungen des Prädikats und der Projektion, wobei result der Rückgabewert ist.

Mögliche Implementierung

struct adjacent_find_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_binary_predicate<
                 std::projected<I, Proj>,
                 std::projected<I, Proj>> Pred = ranges::equal_to>
    constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
    {
        if (first == last)
            return first;
        auto next = ranges::next(first);
        for (; next != last; ++next, ++first)
            if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
                return first;
        return next;
    }
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_binary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>,
                 std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Pred pred = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};
inline constexpr adjacent_find_fn adjacent_find;

Beispiel

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
constexpr bool some_of(auto&& r, auto&& pred) // einige aber nicht alle
{
    return std::ranges::cend(r) != std::ranges::adjacent_find(r,
        [&pred](auto const& x, auto const& y)
        {
            return pred(x) != pred(y);
        });
}
// some_of testen
constexpr auto a = {0, 0, 0, 0}, b = {1, 1, 1, 0}, c = {1, 1, 1, 1};
auto is_one = [](auto x){ return x == 1; };
static_assert(!some_of(a, is_one) && some_of(b, is_one) && !some_of(c, is_one));
int main()
{
    const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
                                ^^          ^^       */
    namespace ranges = std::ranges;
    if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
        std::cout << "Keine benachbarten übereinstimmenden Elemente\n";
    else
        std::cout << "Das erste benachbarte Paar gleicher Elemente befindet sich bei ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
    if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.end())
        std::cout << "Der gesamte Vektor ist in aufsteigender Reihenfolge sortiert\n";
    else
        std::cout << "Das letzte Element in der nicht-abnehmenden Teilfolge befindet sich bei ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}

Ausgabe:

Das erste benachbarte Paar gleicher Elemente befindet sich bei [4] == 40
Das letzte Element in der nicht-abnehmenden Teilfolge befindet sich bei [7] == 41

Siehe auch

Entfernt aufeinanderfolgende doppelte Elemente in einem Bereich
(Algorithmus-Funktionsobjekt)
Findet die ersten zwei benachbarten Elemente, die gleich sind (oder ein gegebenes Prädikat erfüllen)
(Funktionstemplate)