Bibliothèque C ++ Forward_list - fonction forward_list ()

La description

Le constructeur C ++ std::forward_list::forward_list() construit une liste de transfert à partir de la liste d'initialisation.

Déclaration

Voici la déclaration pour l'en-tête std :: forward_list :: forward_list () du constructeur std :: forward_list.

C ++ 11

forward_list (initializer_list<value_type> il,
             const allocator_type& alloc = allocator_type());

Paramètres

  • il - Initialiser la liste.

  • alloc - Objet Allocator.

Valeur de retour

Le constructeur ne renvoie jamais de valeur.

Exceptions

Cette fonction membre ne lève jamais d'exception.

Complexité temporelle

Linéaire ie O (n)

Exemple

L'exemple suivant montre l'utilisation du constructeur std :: forward_list :: forward_list ().

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {
   auto il = {1, 2, 3, 4, 5};
   forward_list<int> fl(il);

   cout << "List contains following elements" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant -

List contains following elements
1
2
3
4
5