Namespaces
Variants

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

Von de.cppreference.net
 
 
Algorithmenbibliothek
Eingeschränkte Algorithmen und Algorithmen auf Bereichen (C++20)
Eingeschränkte Algorithmen, z. B. ranges::copy, ranges::sort, ...
Nicht modifizierende Sequenzoperationen    
Stapeloperationen
(C++17)
Suchoperationen
Modifizierende Sequenzoperationen
Kopieroperationen
(C++11)
(C++11)
Tauschoperationen
Transformationsoperationen
Erzeugungsoperationen
Entfernungsoperationen
Reihenfolgeändernde Operationen
(bis C++17)(C++11)
(C++20)(C++20)
Stichprobenoperationen
(C++17)

Sortier- und verwandte Operationen
Partitionierungsoperationen
(C++11)    

Sortieroperationen
Binäre Suchoperationen
(auf partitionierten Bereichen)
Mengenoperationen (auf sortierten Bereichen)
Mischoperationen (auf sortierten Bereichen)
Heap-Operationen
Minimum/Maximum-Operationen
(C++11)
(C++17)
Lexikografische Vergleichsoperationen
Permutationsoperationen


 
Eingeschränkte Algorithmen
Alle Namen in diesem Menü gehören zum Namensraum std::ranges
Nicht modifizierende Sequenzoperationen
Modifizierende Sequenzoperationen
Partitionierungsoperationen
Sortieroperationen
Binäre Suchoperationen (auf sortierten Bereichen)
       
       
Mengenoperationen (auf sortierten Bereichen)
Heap-Operationen
Minimum/Maximum-Operationen
       
       
Permutationsoperationen
Faltungsoperationen
Numerische Operationen
(C++23)            
Operationen auf nicht initialisiertem Speicher
Rückgabetypen
 
Definiert in Header <algorithm>
Aufrufsignatur
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 = {} );
(1) (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)
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 = {} );
(2) (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)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_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*>
bool contains( Ep&& policy, I first, S last, const T& value, Proj proj = {} );
(5) (seit C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-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*>
bool contains( Ep&& policy, R&& r, const T& value, Proj proj = {} );
(6) (seit C++26)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I1, std::sized_sentinel_for<I1> S1,
          std::random_access_iterator I2, std::sized_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>
bool contains_subrange( Ep&& policy, I1 first1, S1 last1, I2 first2, S2 last2,
                        Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(7) (seit C++26)
template< /*execution-policy*/ Ep,
          /*sized-random-access-range*/ R1, /*sized-random-access-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>
bool contains_subrange( Ep&& policy, R1&& r1, R2&& r2, Pred pred = {},
                        Proj1 proj1 = {}, Proj2 proj2 = {} );
(8) (seit C++26)

Für die Definition von /*execution-policy*/, siehe diese Seite; für die Definition von /*sized-random-access-range*/, siehe diese Seite.

1,2) Überprüft, ob der Quellbereich den Zielwert enthält value.
1) Der Quellbereich ist [first, last).
2) Der Quellbereich ist r.
3,4) Überprüft, ob der Zielbereich ein Teilbereich des Quellbereichs ist.
3) Der Quellbereich ist [first1, last1), und der Zielbereich ist [first2, last2).
4) Der Quellbereich ist r1, und der Zielbereich ist r2.
5-8) Wie (1-4), aber ausgeführt gemäß policy.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithmus-Funktionsobjekte (informell als Niebloids bekannt), das heißt:

Parameter

first/first1, last/last1 - das Iterator-Sentinel-Paar, das den Quell-bereich
definiertfirst2, last2 - das Iterator-Sentinel-Paar, das den Ziel-bereich
definietr/r1 - der Quellbereich
value - der Zielwert
r2 - der Zielbereich
pred/pred1 - das Prädikat, das auf die (projizierten) Elemente im Quellbereich angewendet wird
pred2 - das Prädikat, das auf die (projizierten) Elemente im Zielbereich angewendet wird
proj/proj1 - die Projektion, die auf die Elemente im Quellbereich angewendet wird
proj2 - die Projektion, die auf die Elemente im Zielbereich angewendet wird
policy - die Ausführungsrichtlinie, die verwendet werden soll

Rückgabewert

1) ranges::find(std::move(first), last, value, proj) != last
2) ranges::find(r, value, proj) != ranges::end(r)
3) first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()
4) ranges::empty(r2) || !ranges::search(r1, r2, pred, proj1, proj2).empty()
5-8) Wie (1-4), aber fügt std::forward<Ep>(policy) als erstes Argument zur Argumentliste von ranges::find oder ranges::search hinzu.

Komplexität

Gegeben

  • N als ranges::distance(first, last) oder ranges::distance(r),
  • N1 als ranges::distance(first1, last1) oder ranges::distance(r1), und
  • N2 als ranges::distance(first2, last2) oder ranges::distance(r2):
1,2) Höchstens N Vergleiche und Anwendungen von proj.
3,4) Höchstens N1·N2 Anwendungen von pred und proj.
5,6) 𝓞(N) Vergleiche und Anwendungen von proj.
7,8) 𝓞(N1·N2) Anwendungen von pred und proj.

Ausnahmen

5-8) Während des Ausführungsprozesses:
  • Wenn die für die Parallelisierung erforderlichen temporären Speicherressourcen nicht verfügbar sind, wird std::bad_alloc geworfen.
  • Wenn eine nicht gefangene Ausnahme beim Zugriff auf Objekte über ein Algorithmus-Argument geworfen wird, wird das Verhalten durch die Ausführungsrichtlinie bestimmt (für Standardrichtlinien wird std::terminate aufgerufen).

Hinweise

In C++20 kann man contains mit ranges::find(haystack, needle) != ranges::end(haystack) oder contains_subrange mit !ranges::search(haystack, needle).empty() implementieren.

ranges::contains_subrange hat, wie ranges::search, und im Gegensatz zu std::search, keine Unterstützung für Sucher (wie std::boyer_moore_searcher).

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

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 ranges::find(r, value, proj) != ranges::end(r);
    }
};

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 ranges::empty(r2) ||
                   !ranges::search(r1, r2, pred, proj1, proj2).empty();
    }
};

inline constexpr contains_subrange_fn contains_subrange{};

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
}

Siehe auch

findet das erste Element, das bestimmte Kriterien erfüllt
(Algorithmus-Funktionsobjekt)
sucht nach dem ersten Vorkommen eines Bereichs von Elementen
(Algorithmus-Funktionsobjekt)
bestimmt, ob ein Element in einem Bereich mithilfe binärer Suche existiert
(Algorithmus-Funktionsobjekt)
bestimmt, ob eine Sequenz eine Teilsequenz einer anderen ist
(Algorithmus-Funktionsobjekt)
prüft, ob ein Prädikat true für alle, mindestens eines oder keines der Elemente in einem Bereich zutrifft
(Algorithmus-Funktionsobjekt)