C++ set swap () function | HTML KICK

C++ set swap function

C++ set swap()

This is a function used in swapping or exchanging content of 2 sets though both sets need to be of a similar kind. Still, the sizes can differ.

C++ set swap () function with Examples

Syntax

void swap (set& x);

Parameter

x: this is a set container to interchange the contents with.

Return value

It’s none

Complexity

It’s constant

Iterator validity

Every references, iterators & pointer speak of elements in all set containers continues to be valid. However, we are speaking of the elements in other set containers, & iterating in them.

Exception Safety

No result on the container if the exceptions are thrown.

Data Races

All the containers & x are improved.

Example 1

With this simple example, we need to swap the element of a single set to the other

#include <iostream>  
#include <set>  
  
using namespace std;  
  
int main(void) {  
   set<int> m1 = {1,2,3,4,5};  
  
  
   set<int> m2;  
  
   m2.swap(m1);  
  
   cout << "Set contains following elements" << endl;  
  
   for (auto it = m2.begin(); it != m2.end(); ++it)  
      cout << *it<< endl;  
  
   return 0;  
}

Results

Set contains following elements
1
2
3
4
5

In the example above, set m1 consist of five elements & m2 is void. When one swaps m1 to m2 all the features of m1 will be moved to m2.

Example 2

A simple example for exchanging the content of 2 sets:

#include <iostream>  
#include <set>  
  
using namespace std;  
  
 int main () {  
   int myints[] = {10,20,30,40,50,60};  
   set<int> first (myints,myints+3);  
   set<int> second (myints+3,myints+6);    
  
   first.swap(second);  
  
   cout << "first set contains:";  
   for (set<int>::iterator it = first.begin(); it!=first.end(); ++it)  
      cout << ' ' << *it;  
   cout << '\n';  
  
   cout << "second set contains:";  
   for (set<int>::iterator it = second.begin(); it!=second.end(); ++it)  
      cout << ' ' << *it;  
   cout << '\n';  
  
   return 0;  
}

Results

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

Example 3

A simple example for swapping contents of 2 sets:

#include<iostream>  
#include<set>  
using namespace std;  
   
int main()  
{  
    // Take any two sets  
    set<char> set1, set2;  
      
    set1 = {'a','b','c','d'};   
    set2 = {'x','y','z'};  
   
    // Swap elements of sets  
    swap(set1, set2);  
   
    // Print the elements of sets  
    cout << "set1:\n";  
    for (auto it = set1.begin(); it != set1.end(); it++)  
        cout << "\t" << *it<< '\n';  
   
    cout << "set2:\n";  
    for (auto it = set2.begin(); it != set2.end(); it++)  
        cout << "\t" << *it<< '\n';  
   
    return 0;  
}

Results

set1:
  x
  y
  z
set2:
  a
  b
  c
  d

In the example above, another way of swap() function is utilized to swap c++ contents of 2 sets.

Example 4

#include <set>    
#include <iostream>    
    
int main( )    
{    
   using namespace std;    
   set <int> s1, s2, s3;    
   set <int>::iterator s1_Iter;    
    
   s1.insert( 10 );    
   s1.insert( 20 );    
   s1.insert( 30 );    
   s2.insert( 100 );    
   s2.insert( 200 );    
   s3.insert( 300 );    
    
   cout << "The original set s1 is:";    
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )    
      cout << " " << *s1_Iter;    
   cout   << "." << endl;    
    
   // This is the member function version of swap    
   s1.swap( s2 );    
    
   cout << "After swapping with s2, list s1 is:";    
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )    
      cout << " " << *s1_Iter;    
   cout  << "." << endl;    
    
   // This is the specialized template version of swap    
   swap( s1, s3 );    
    
   cout << "After swapping with s3, list s1 is:";    
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )    
      cout << " " << *s1_Iter;    
   cout   << "." << endl;    
}

Results

The original set s1 is: 10 20 30.
After swapping with s2, list s1 is: 100 200.
After swapping with s3, list s1 is: 300.

Doctype Html html5

How to define Head in HTML

What is HTML Elements in HTML5?

What’s the Difference between HTML5 and HTML?

Terry White

Terry White is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

View all posts by Terry White →

Leave a Reply

Your email address will not be published. Required fields are marked *