Namespaces
Variants

std::forward_list<T,Allocator>:: forward_list

From cppreference.net
forward_list ( ) : forward_list ( Allocator ( ) ) { }
(1) (constexpr seit C++26)
explicit forward_list ( const Allocator & alloc ) ;
(2) (constexpr seit C++26)
explicit forward_list ( size_type count,
const Allocator & alloc = Allocator ( ) ) ;
(3) (constexpr seit C++26)
forward_list ( size_type count, const T & value,
const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr seit C++26)
template < class InputIt >

forward_list ( InputIt first, InputIt last,

const Allocator & alloc = Allocator ( ) ) ;
(5) (constexpr seit C++26)
template < container-compatible-range < T > R >

forward_list ( std:: from_range_t , R && rg,

const Allocator & alloc = Allocator ( ) ) ;
(6) (seit C++23)
(constexpr seit C++26)
forward_list ( const forward_list & other ) ;
(7) (constexpr seit C++26)
forward_list ( forward_list && other ) ;
(8) (constexpr seit C++26)
(9)
forward_list ( const forward_list & other, const Allocator & alloc ) ;
(bis C++23)
forward_list ( const forward_list & other,
const std:: type_identity_t < Allocator > & alloc ) ;
(seit C++23)
(constexpr seit C++26)
(10)
forward_list ( forward_list && other, const Allocator & alloc ) ;
(bis C++23)
forward_list ( forward_list && other,
const std:: type_identity_t < Allocator > & alloc ) ;
(seit C++23)
(constexpr seit C++26)
forward_list ( std:: initializer_list < T > init,
const Allocator & alloc = Allocator ( ) ) ;
(11) (constexpr seit C++26)

Konstruiert eine neue forward_list aus verschiedenen Datenquellen, optional unter Verwendung einer benutzerdefinierten Zuweisung alloc .

1) Der Standardkonstruktor. Konstruiert eine leere forward_list mit einem standardkonstruierten Allokator.
Wenn Allocator nicht DefaultConstructible ist, ist das Verhalten undefiniert.
2) Konstruiert eine leere forward_list mit dem gegebenen Allokator alloc .
3) Konstruiert eine forward_list mit count standardmäßig eingefügten Objekten von T . Es werden keine Kopien erstellt.
Wenn T nicht DefaultInsertable in forward_list ist, ist das Verhalten undefiniert.
4) Konstruiert eine forward_list mit count Kopien der Elemente mit dem Wert value .
Wenn T nicht CopyInsertable in forward_list ist, ist das Verhalten undefiniert.
5) Konstruiert eine forward_list mit den Inhalten des Bereichs [ first , last ) . Jeder Iterator in [ first , last ) wird genau einmal dereferenziert.
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn InputIt die Anforderungen von LegacyInputIterator erfüllt.
Wenn T nicht EmplaceConstructible in forward_list von * first ist, ist das Verhalten undefiniert.
6) Konstruiert eine forward_list mit den Inhalten des Bereichs rg . Jeder Iterator in rg wird genau einmal dereferenziert.
Wenn T nicht EmplaceConstructible in forward_list von * ranges:: begin ( rg ) ist, ist das Verhalten undefiniert.
7) Der Kopierkonstruktor. Konstruiert eine forward_list mit den Inhalten von other . Der Allokator wird erhalten, als ob durch Aufruf von std:: allocator_traits < Allocator > :: select_on_container_copy_construction
( other. get_allocator ( ) )
.
8) Der Move-Konstruktor. Konstruiert eine forward_list mit den Inhalten von other . Der Allokator wird durch Move-Konstruktion von other. get_allocator ( ) erhalten.
9) Gleich wie der Kopierkonstruktor, außer dass alloc als Allokator verwendet wird.
Wenn T nicht CopyInsertable in forward_list ist, ist das Verhalten undefiniert.
10) Gleich wie der Move-Konstruktor, außer dass alloc als Allokator verwendet wird.
Wenn T nicht MoveInsertable in forward_list ist, ist das Verhalten undefiniert.
11) Entspricht forward_list ( il. begin ( ) , il. end ( ) , alloc ) .

Inhaltsverzeichnis

Parameter

alloc - Allokator, der für alle Speicherallokationen dieses Containers verwendet wird
count - die Größe des Containers
value - der Wert, mit dem Elemente des Containers initialisiert werden
first, last - das Iteratorenpaar, das den Quell- Bereich der zu kopierenden Elemente definiert
other - ein weiterer Container, der als Quelle zur Initialisierung der Elemente des Containers verwendet wird
init - Initialisierungsliste zur Initialisierung der Elemente des Containers
rg - ein containerkompatibler Bereich

Komplexität

1,2) Konstant.
3,4) Linear in count .
5) Linear in std:: distance ( first, last ) .
6) Linear in ranges:: distance ( rg ) .
7) Linear in other. size ( ) .
8) Konstante.
9) Linear in other. size ( ) .
10) Linear in other. size ( ) if alloc ! = other. get_allocator ( ) , ansonsten konstant.
11) Linear in init. size ( ) .

Ausnahmen

Aufrufe von Allocator :: allocate können eine Exception werfen.

Hinweise

Nach Container-Verschiebekonstruktion (Überladung ( 8 ) ) bleiben Referenzen, Zeiger und Iteratoren (außer dem End-Iterator) auf other gültig, verweisen jedoch auf Elemente, die sich nun in * this befinden. Der aktuelle Standard gibt diese Garantie durch die pauschale Aussage in [container.reqmts]/67 , und eine direktere Garantie wird durch LWG issue 2321 in Betracht gezogen.

Feature-Test Makro Wert Std Funktion
__cpp_lib_containers_ranges 202202L (C++23) Ranges-bewusste Konstruktion und Einfügung; Überladung ( 6 )

Beispiel

#include <forward_list>
#include <iostream>
#include <string>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::forward_list<T>& v)
{
    s.put('{');
    for (char comma[]{'\0', ' ', '\0'}; const auto& e : v)
        s << comma << e, comma[0] = ',';
    return s << "}\n";
}
int main()
{
    // C++11 Initialisierungslisten-Syntax:
    std::forward_list<std::string> words1{"the", "frogurt", "is", "also", "cursed"};
    std::cout << "1: " << words1;
    // words2 == words1
    std::forward_list<std::string> words2(words1.begin(), words1.end());
    std::cout << "2: " << words2;
    // words3 == words1
    std::forward_list<std::string> words3(words1);
    std::cout << "3: " << words3;
    // words4 ist {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::forward_list<std::string> words4(5, "Mo");
    std::cout << "4: " << words4;
    const auto rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::forward_list<std::string> words5(std::from_range, rg); // Überladung (6)
#else
    std::forward_list<std::string> words5(rg.begin(), rg.end()); // Überladung (5)
#endif
    std::cout << "5: " << words5;
}

Ausgabe:

1: {the, frogurt, is, also, cursed}
2: {the, frogurt, is, also, cursed}
3: {the, frogurt, is, also, cursed}
4: {Mo, Mo, Mo, Mo, Mo}
5: {cat, cow, crow}

Fehlerberichte

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

DR Angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
LWG 2193 C++11 der Standardkonstruktor war explicit als non-explicit festgelegt
LWG 2210 C++11 Überladung ( 3 ) hatte keinen Allokator-Parameter Parameter hinzugefügt
N3346 C++11 für Überladung ( 3 ) wurden die Elemente im
Container value-initialized
sie sind default-inserted

Siehe auch

weist dem Container Werte zu
(öffentliche Elementfunktion)
weist dem Container Werte zu
(öffentliche Elementfunktion)