A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_set_swap.htm below:

C++ Set Library - swap Function

C++ Set Library - swap Function 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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4