Libreria di stringhe C ++ - swap
Descrizione
Scambia il contenuto del contenitore con il contenuto di str, che è un altro oggetto stringa. Le lunghezze possono differire.
Dichiarazione
Di seguito è riportata la dichiarazione per std :: string :: swap.
void swap (string& str);C ++ 11
void swap (string& str);C ++ 14
void swap (string& str);Parametri
str - È un oggetto stringa.
Valore di ritorno
nessuna
Eccezioni
se viene generata un'eccezione, non ci sono modifiche nella stringa.
Esempio
Nell'esempio seguente per std :: string :: swap.
#include <iostream>
#include <string>
main () {
   std::string buyer ("money");
   std::string seller ("goods");
   std::cout << "Before the swap, buyer has " << buyer;
   std::cout << " and seller has " << seller << '\n';
   seller.swap (buyer);
   std::cout << " After the swap, buyer has " << buyer;
   std::cout << " and seller has " << seller << '\n';
   return 0;
}L'output di esempio dovrebbe essere così:
Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money