Namespaces
Variants

std:: includes

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
Definiert in Header <algorithm>
template < class InputIt1, class InputIt2 >

bool includes ( InputIt1 first1, InputIt1 last1,

InputIt2 first2, InputIt2 last2 ) ;
(1) (constexpr seit C++20)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
bool includes ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, ForwardIt2 last2 ) ;
(2) (seit C++17)
template < class InputIt1, class InputIt2, class Compare >

bool includes ( InputIt1 first1, InputIt1 last1,

InputIt2 first2, InputIt2 last2, Compare comp ) ;
(3) (constexpr seit C++20)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class Compare >
bool includes ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, ForwardIt2 last2, Compare comp ) ;
(4) (seit C++17)

Gibt true zurück, falls der sortierte Bereich [ first2 , last2 ) eine Teilfolge des sortierten Bereichs [ first1 , last1 ) ist (eine Teilfolge muss nicht zusammenhängend sein).

1) Falls [ first1 , last1 ) oder [ first2 , last2 ) nicht sortiert ist bezüglich operator < (bis C++20) std:: less { } (seit C++20) , ist das Verhalten undefiniert.
3) Falls [ first1 , last1 ) oder [ first2 , last2 ) nicht bezüglich comp sortiert ist, ist das Verhalten undefiniert.
2,4) Gleich wie (1,3) , aber ausgeführt gemäß policy .
Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn alle folgenden Bedingungen erfüllt sind:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> ist true .

(bis C++20)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> ist true .

(seit C++20)

Inhaltsverzeichnis

Parameter

first1, last1 - das Paar von Iteratoren, das den sortierten Bereich der zu untersuchenden Elemente definiert
first2, last2 - das Paar von Iteratoren, das den sortierten Bereich der zu suchenden Elemente definiert
policy - die zu verwendende Ausführungsrichtlinie
comp - Vergleichsfunktionsobjekt (d.h. ein Objekt, das die Anforderungen von Compare erfüllt), das ​ true zurückgibt, wenn das erste Argument kleiner als (d.h. vor dem zweiten sortiert ist) das zweite Argument ist.

Die Signatur der Vergleichsfunktion sollte äquivalent zu Folgendem sein:

bool cmp ( const Type1 & a, const Type2 & b ) ;

Obwohl die Signatur nicht const & benötigt, darf die Funktion die übergebenen Objekte nicht modifizieren und muss alle Werte des Typs (möglicherweise const) Type1 und Type2 unabhängig von der Wertkategorie akzeptieren können (daher ist Type1& nicht erlaubt , ebenso wenig wie Type1 , es sei denn, für Type1 ist eine Verschiebung äquivalent zu einer Kopie (seit C++11) ).
Die Typen Type1 und Type2 müssen so beschaffen sein, dass ein Objekt vom Typ InputIt dereferenziert und dann implizit in beide konvertiert werden kann. ​

Typanforderungen
-
InputIt1, InputIt2 müssen die Anforderungen von LegacyInputIterator erfüllen.
-
ForwardIt1, ForwardIt2 müssen die Anforderungen von LegacyForwardIterator erfüllen.
-
Compare müssen die Anforderungen von Compare erfüllen.

Rückgabewert

true falls [ first2 , last2 ) eine Teilfolge von [ first1 , last1 ) ist; andernfalls false .

Eine leere Sequenz ist eine Teilsequenz jeder Sequenz, daher wird true zurückgegeben, falls [ first2 , last2 ) leer ist.

Komplexität

Gegeben N 1 als std:: distance ( first1, last1 ) und N 2 als std:: distance ( first2, last2 ) :

1,2) Höchstens 2⋅(N 1 +N 2 )-1 Vergleiche unter Verwendung von operator < (bis C++20) std:: less { } (seit C++20) .
3,4) Höchstens 2⋅(N 1 +N 2 )-1 Anwendungen der Vergleichsfunktion comp .

Ausnahmen

Die Überladungen mit einem Template-Parameter namens ExecutionPolicy melden Fehler wie folgt:

  • Wenn die Ausführung einer als Teil des Algorithmus aufgerufenen Funktion eine Exception wirft und ExecutionPolicy einer der Standard-Policies ist, wird std::terminate aufgerufen. Für jede andere ExecutionPolicy ist das Verhalten implementierungsdefiniert.
  • Wenn der Algorithmus keinen Speicher allozieren kann, wird std::bad_alloc geworfen.

Mögliche Implementierung

include (1)
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2)
{
    for (; first2 != last2; ++first1)
    {
        if (first1 == last1 || *first2 < *first1)
            return false;
        if (!(*first1 < *first2))
            ++first2;
    }
    return true;
}
include (3)
template<class InputIt1, class InputIt2, class Compare>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2, Compare comp)
{
    for (; first2 != last2; ++first1)
    {
        if (first1 == last1 || comp(*first2, *first1))
            return false;
        if (!comp(*first1, *first2))
            ++first2;
    }
    return true;
}

Beispiel

#include <algorithm>
#include <cctype>
#include <iostream>
template<class Os, class Co>
Os& operator<<(Os& os, const Co& v)
{
    for (const auto& i : v)
        os << i << ' ';
    return os << '\t';
}
int main()
{
    const auto
        v1 = {'a', 'b', 'c', 'f', 'h', 'x'},
        v2 = {'a', 'b', 'c'},
        v3 = {'a', 'c'},
        v4 = {'a', 'a', 'b'},
        v5 = {'g'},
        v6 = {'a', 'c', 'g'},
        v7 = {'A', 'B', 'C'};
    auto no_case = [](char a, char b) { return std::tolower(a) < std::tolower(b); };
    std::cout
    << v1 << "\nincludes:\n" << std::boolalpha
    << v2 << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n'
    << v3 << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n'
    << v4 << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n'
    << v5 << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n'
    << v6 << ": " << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end()) << '\n'
    << v7 << ": " << std::includes(v1.begin(), v1.end(), v7.begin(), v7.end(), no_case)
          << " (case-insensitive)\n";
}

Ausgabe:

a b c f h x
includes:
a b c   : true
a c     : true
a a b   : false
g       : false
a c g   : false
A B C   : true (case-insensitive)

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 1205 C++98 der Rückgabewert war unklar, falls [ first2 , last2 ) leer ist gibt in diesem Fall true zurück

Siehe auch

berechnet die Differenz zwischen zwei Mengen
(Funktions-Template)
sucht nach dem ersten Vorkommen eines Elementbereichs
(Funktions-Template)
gibt true zurück, wenn eine Sequenz eine Teilsequenz einer anderen ist
(Algorithmus-Funktionsobjekt)