Libreria Forward_list C ++ - funzione clear ()

Descrizione

La funzione C ++ std::forward_list::clear() distrugge forward_list rimuovendo tutti gli elementi da forward_list e imposta la dimensione di forward_list su zero.

Dichiarazione

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

C ++ 11

void clear() 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 :: clear ().

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   if (!fl.empty())
      cout << "List is non−empty before clear operation." << endl;

   fl.clear();

   if (fl.empty())
      cout << "List is empty after clear operation." << endl;

   return 0;
}

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

List is non-empty before clear operation.
List is empty after clear operation.