C++ Set Library - swap Function


Advertisements

Description

It exchanges the content of the container by the content of x.

Declaration

Following are the ways in which std::set::swap works in various C++ versions.

C++98

void swap (set& x);

C++11

void swap (set& x);

Return value

none

Exceptions

It never throws exception.

Time complexity

Time complexity is constant.

Example

The following example shows the usage of 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;
}

The above program will compile and execute properly.

first contains: 40 50 60
second contains: 10 20 30
set.htm
Advertisements