C++ set swap function | HTML KICK

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.

The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. This function is used to exchange the contents of two sets but the sets must be of the same type, although sizes may differ.

FAQ

Is there a swap function in C++?

swap() in C++

The function std::swap() is a built-in function in the C++ Standard Template Library (STL) that swaps the value of two variables.

How do you change elements in a set?

Let’s see the simple example to swap the element of one set to another:
#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;

What is a swap in C programming?

Swapping two numbers in the C programming language means exchanging the values of two variables. Suppose you have two variables var1 & var2. Value of var1 is 20 & value of var2 is 40. So, after swapping the value of var1 will become 40 & value of var 2 will become 20.

Why is C++ so difficult?

C++ is known to be one of the most difficult programming languages to learn over other popular languages like Python and Java. C++ is hard to learn because of its multi-paradigm nature and more advanced syntax.

How many days we can learn C++?

3 months

Doctype Html html5

How to define Head in HTML

What is HTML Elements in HTML5?

What’s the Difference between HTML5 and HTML?

Leave a Comment