Libreria C ++ Forward_list - funzione reverse ()

Descrizione

La funzione C ++ std::forward_list::reverse() inverte l'ordine degli elementi presenti in forward_list.

Dichiarazione

Di seguito è riportata la dichiarazione per la funzione std :: forward_list :: reverse () nell'intestazione std :: forward_list.

C ++ 11

void reverse() noexcept;

Parametri

Nessuna

Valore di ritorno

Nessuna

Eccezioni

Questa funzione membro non genera mai eccezioni.

Complessità temporale

Lineare cioè O (n)

Esempio

L'esempio seguente mostra l'utilizzo della funzione std :: forward_list :: reverse ().

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   fl.reverse();

   cout << "List contents after reverse operation" << endl;

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

   return 0;
}

Compiliamo ed eseguiamo il programma sopra, questo produrrà il seguente risultato:

List contents after reverse operation
5
4
3
2
1