Swap two sets using the swap() function : set swap « set multiset « C++ Tutorial






#include <set>
#include <iostream>
#include <string>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main(){
  set<string> first;

  first.insert("A");
  first.insert("B");
  first.insert("C");

  cout << first.size() << endl;
  set<string> second (first);   // Copy constructor

  second.insert("I");
  second.insert("S");

  swap(first, second);

  set<string> third = first;

  print(first);
  print(second);
  print(third);
}








19.9.set swap
19.9.1.Swap two sets and display its content
19.9.2.swap one set with another set
19.9.3.Swap two sets using the swap() function