Bibliothèque de chaînes C ++ - swap

La description

Il échange le contenu du conteneur par le contenu de str, qui est un autre objet string. Les longueurs peuvent différer.

Déclaration

Voici la déclaration pour std :: string :: swap.

void swap (string& str);

C ++ 11

void swap (string& str);

C ++ 14

void swap (string& str);

Paramètres

str - C'est un objet string.

Valeur de retour

aucun

Exceptions

si une exception est levée, il n'y a aucun changement dans la chaîne.

Exemple

Dans l'exemple ci-dessous pour 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'exemple de sortie devrait être comme ceci -

Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money