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

La description

La fonction C ++ std::forward_list::splice_after()transfère l'élément pointé par l'itérateur i de forward_list x dans * this . L'élément est inséré après l'élément pointé par sa position .

Déclaration

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

C ++ 11

void splice_after (const_iterator position, forward_list& x, const_iterator i);

Paramètres

  • position - Position dans la forward_list après laquelle les nouveaux éléments à insérer.

  • x - Un autre objet forward_list du même type.

  • i - Itérateur à accès aléatoire.

Valeur de retour

Aucun

Des exceptions

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

Complexité temporelle

Linéaire ie O (n)

Exemple

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   fl2.splice_after(fl2.begin(), fl1, fl1.begin());

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

   for (auto it = fl2.begin(); it != fl2.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