Namensräume
Varianten
Aktionen

std::ranges::contains, std::ranges::contains_subrange

Von cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithmenbibliothek
Beschränkte Algorithmen und Algorithmen für Bereiche (C++20)
Beschränkte Algorithmen, z.B. ranges::copy, ranges::sort, ...
Ausführungsrichtlinien (C++17)
Nicht-modifizierende Sequenzoperationen
Stapeloperationen
(C++17)
Suchoperationen
(C++11)                (C++11)(C++11)

Modifizierende Sequenzoperationen
Kopieroperationen
(C++11)
(C++11)
Tauschoperationen
Transformationsoperationen
Generierungsoperationen
Entfernungsoperationen
Ordnungsändernde Operationen
(bis C++17)(C++11)
(C++20)(C++20)
Stichprobenoperationen
(C++17)

Sortier- und verwandte Operationen
Partitionierungsoperationen
Sortieroperationen
Binäre Suchoperationen
(auf partitionierten Bereichen)
Mengenoperationen (auf sortierten Bereichen)
Zusammenführungsoperationen (auf sortierten Bereichen)
Heapoperationen
Minimum/Maximum-Operationen
(C++11)
(C++17)
Lexikographische Vergleichsoperationen
Permutationsoperationen
C-Bibliothek
Numerische Operationen
Operationen auf uninitialisiertem Speicher
 
Eingeschränkte Algorithmen
Alle Namen in diesem Menü gehören zum Namespace std::ranges
Nicht-modifizierende Sequenzoperationen
Modifizierende Sequenzoperationen
Partitionierungsoperationen
Sortieroperationen
Binäre Suchoperationen (auf sortierten Bereichen)
       
       
Mengenoperationen (auf sortierten Bereichen)
Heapoperationen
Minimum/Maximum-Operationen
       
       
Permutationsoperationen
Faltoperationen
Operationen auf uninitialisiertem Speicher
Rückgabetypen
 
Definiert in Header <algorithm>
Aufruf-Signatur
(1)
template< std::input_iterator I, std::sentinel_for<I> S,

          class T,
          class Proj = std::identity >
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                        const T*>

constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
(seit C++23)
(bis C++26)
template< std::input_iterator I, std::sentinel_for<I> S,

          class Proj = std::identity,
          class T = std::projected_value_t<I, Proj> >
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                        const T*>

constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
(seit C++26)
(2)
template< ranges::input_range R,

          class T,
          class Proj = std::identity >
requires std::indirect_binary_predicate<ranges::equal_to,
                                        std::projected<ranges::iterator_t<R>, Proj>,
                                        const T*>

constexpr bool contains( R&& r, const T& value, Proj proj = {} );
(seit C++23)
(bis C++26)
template< ranges::input_range R,

          class Proj = std::identity,
          class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::indirect_binary_predicate<ranges::equal_to,
                                        std::projected<ranges::iterator_t<R>, Proj>,
                                        const T*>

constexpr bool contains( R&& r, const T& value, Proj proj = {} );
(seit C++26)
template< std::forward_iterator I1, std::sentinel_for<I1> S1,

          std::forward_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 bool contains_subrange( I1 first1, S1 last1, I2 first2, S2 last2,
                                  Pred pred = {},

                                  Proj1 proj1 = {}, Proj2 proj2 = {} );
(3) (seit C++23)
template< ranges::forward_range R1, ranges::forward_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 bool contains_subrange( R1&& r1, R2&& r2, Pred pred = {},

                                  Proj1 proj1 = {}, Proj2 proj2 = {} );
(4) (seit C++23)
1) Suchalgorithmus, der prüft, ob ein gegebener Bereich einen Wert mit Iterator-Sentinel-Paaren enthält.
2) Dasselbe wie in (1), verwendet jedoch r als Quellbereich, als ob ranges::begin(r) als first und ranges::end(r) als last verwendet werden.
3) Suchalgorithmus, der prüft, ob ein gegebener Bereich ein Teilbereich eines anderen Bereichs mit Iterator-Sentinel-Paaren ist.
4) Dasselbe wie in (3), verwendet jedoch r1 als ersten Quellbereich und r2 als zweiten Quellbereich, als ob ranges::begin(r1) als first1, ranges::end(r1) als last1, ranges::begin(r2) als first2 und ranges::end(r2) als last2 verwendet werden.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithmus-Funktionsobjekte (informell als niebloids bekannt), d.h.

Inhalt

[bearbeiten] Parameter

first, last - das Iterator-Sentinel-Paar, das den Bereich der zu untersuchenden Elemente definiert
r - der zu untersuchende Elementbereich
value - Wert, mit dem die Elemente verglichen werden sollen
pred - Prädikat, das auf die projizierten Elemente angewendet wird
proj - Projektion, die auf die Elemente angewendet wird

[bearbeiten] Rückgabewert

1,2) : ranges::find(std::move(first), last, value, proj) != last
3,4) : first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()

[bearbeiten] Komplexität

Maximal last - first Anwendungen des Prädikats und der Projektion.

[bearbeiten] Anmerkungen

Bis C++20 mussten wir std::ranges::find(r, value) != std::ranges::end(r) verwenden, um zu prüfen, ob ein einzelner Wert in einem Bereich enthalten ist. Und um zu prüfen, ob ein Bereich einen Teilbereich von Interesse enthält, verwenden wir not std::ranges::search(haystack, needle).empty(). Dies ist zwar korrekt, aber nicht unbedingt praktisch und drückt die Absicht kaum aus (insbesondere im letzteren Fall). Die Möglichkeit, std::ranges::contains(r, value) zu sagen, adressiert beide Punkte.

ranges::contains_subrange, wie ranges::search, aber im Gegensatz zu std::search bietet es keinen Zugriff auf Searcher (wie Boyer-Moore).

Feature-Test-Makro Wert Std Feature
__cpp_lib_ranges_contains 202207L (C++23) std::ranges::contains und ranges::contains_subrange
__cpp_lib_algorithm_default_value_type 202403 (C++26) Listeninitialisierung für Algorithmen (1,2)

[bearbeiten] Mögliche Implementierung

contains (1,2)
struct __contains_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                            const T*>
    constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        return ranges::find(std::move(first), last, value, proj) != last;
    }
 
    template<ranges::input_range R,
             class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::indirect_binary_predicate<ranges::equal_to,
                                            std::projected<ranges::iterator_t<R>, Proj>,
                                            const T*>
    constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(value), proj);
    }
};
 
inline constexpr __contains_fn contains {};
contains_subrange (3,4)
struct __contains_subrange_fn
{
    template<std::forward_iterator I1, std::sentinel_for<I1> S1,
             std::forward_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 bool operator()(I1 first1, S1 last1,
                              I2 first2, S2 last2,
                              Pred pred = {},
                              Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (first2 == last2) ||
               !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty();
    }
 
    template<ranges::forward_range R1, ranges::forward_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 bool 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::move(pred),
                       std::move(proj1), std::move(proj2));
    }
};
 
inline constexpr __contains_subrange_fn contains_subrange {};

[bearbeiten] Beispiel

#include <algorithm>
#include <array>
#include <complex>
 
namespace ranges = std::ranges;
 
int main()
{
    constexpr auto haystack = std::array{3, 1, 4, 1, 5};
    constexpr auto needle = std::array{1, 4, 1};
    constexpr auto bodkin = std::array{2, 5, 2};
 
    static_assert(
        ranges::contains(haystack, 4) &&
       !ranges::contains(haystack, 6) &&
        ranges::contains_subrange(haystack, needle) &&
       !ranges::contains_subrange(haystack, bodkin)
    );
 
    constexpr std::array<std::complex<double>, 3> nums{{{1, 2}, {3, 4}, {5, 6}}};
    #ifdef __cpp_lib_algorithm_default_value_type
        static_assert(ranges::contains(nums, {3, 4}));
    #else
        static_assert(ranges::contains(nums, std::complex<double>{3, 4}));
    #endif
}

[bearbeiten] Siehe auch

Findet das erste Element, das bestimmte Kriterien erfüllt
(Algorithmus-Funktionsobjekt)[edit]
Sucht nach dem ersten Vorkommen eines Elementbereichs
(Algorithmus-Funktionsobjekt)[edit]
Stellt fest, ob ein Element in einem teilweise geordneten Bereich vorhanden ist
(Algorithmus-Funktionsobjekt)[edit]
Gibt true zurück, wenn eine Sequenz eine Untersequenz einer anderen ist
(Algorithmus-Funktionsobjekt)[edit]
Prüft, ob eine Bedingung für alle, einige oder keine Elemente in einem Bereich wahr ist
(Algorithmus-Funktionsobjekt)[edit]