Libreria stack C ++ - operator = Function

Descrizione

La funzione C ++ std::stack::operator=assegna nuovi contenuti allo stack sostituendo quelli vecchi. Questo metodo modifica la dimensione dello stack, se necessario.

Dichiarazione

Di seguito è riportata la dichiarazione per std :: stack :: operator = function form std :: stack header.

C ++ 11

stack<T, Container>& 
operator=( stack<T,Container>&& other );

Parametri

x - Un altro oggetto stack dello stesso tipo.

Valore di ritorno

Restituisce questo puntatore.

Eccezioni

Questa funzione membro non genera mai eccezioni.

Complessità temporale

Lineare cioè O (n)

Esempio

L'esempio seguente mostra l'utilizzo di std :: stack :: operator = function.

#include <iostream>
#include <stack>

using namespace std;

int main(void) {
   stack<int> s1;
   stack<int> s2;

   for (int i = 0; i < 5; ++i)
      s1.push(i + 1);

   cout << "Size of stack s1 before move operation = " << s1.size() << endl;

   s2 = move(s1);

   cout << "Size of stack s1 after move operation = " << s1.size() << endl;

   cout << "Contents of stack s2" << endl;
   while (!s2.empty()) {
      cout << s2.top() << endl;
      s2.pop();
   }

   return 0;
}

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

Size of stack s1 before move operation = 5
Size of stack s1 after move operation = 0
Contents of stack s2
5
4
3
2
1