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

La description

La fonction C ++ std::forward_list::begin() renvoie un itérateur à accès aléatoire qui pointe vers le premier élément de la forward_list.

Déclaration

Voici la déclaration de l'en-tête std :: forward_list :: begin () de la fonction std :: forward_list.

C ++ 11

iterator begin() noexcept;
const_iterator begin() const noexcept;

Paramètres

Aucun

Valeur de retour

Si l'objet forward_list est qualifié de constante, la méthode retourne un itérateur d'accès aléatoire constant, sinon un itérateur d'accès aléatoire non constant.

Exceptions

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

Complexité temporelle

Constante ie O (1)

Exemple

L'exemple suivant montre l'utilisation de la fonction std :: forward_list :: begin ().

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 3, 4, 5};

   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