Bibliothèque d'ensemble C ++ - fonction swap

La description

Il échange le contenu du conteneur par le contenu de x.

Déclaration

Voici les façons dont std :: set :: swap fonctionne dans différentes versions de C ++.

C ++ 98

void swap (set& x);

C ++ 11

void swap (set& x);

Valeur de retour

aucun

Des exceptions

Il ne jette jamais d'exception.

Complexité temporelle

La complexité temporelle est constante.

Exemple

L'exemple suivant montre l'utilisation de std :: set :: swap.

#include <iostream>
#include <set>

main () {
   int myints[] = {10,20,30,40,50,60};
   std::set<int> first (myints,myints+3);
   std::set<int> second (myints+3,myints+6);  

   first.swap(second);

   std::cout << "first contains:";
   for (std::set<int>::iterator it = first.begin(); it!=first.end(); ++it)
      std::cout << ' ' << *it;
   std::cout << '\n';

   std::cout << "second contains:";
   for (std::set<int>::iterator it = second.begin(); it!=second.end(); ++it)
      std::cout << ' ' << *it;
   std::cout << '\n';

   return 0;
}

Le programme ci-dessus se compilera et s'exécutera correctement.

first contains: 40 50 60
second contains: 10 20 30