TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set swap() Function

C++ set swap() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ set swap()

C++ swap() function is used to swap (or exchange) the content of two sets but both the sets must be of same type although sizes may differ.

Syntax

void swap (set& x);

Parameter

x: set container to exchange the contents with.

Return value

None

Complexity

Constant.

Iterator validity

All references, iterators and pointers referring to elements in both set containers remain valid, but now are referring to elements in the other set container, and iterate in it.

Data Races

Both the container and x are modified.

Exception Safety

No effect on container if exception is thrown.

Example 1

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;

   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << *it<< endl;

   return 0;
}

Output:

Set contains following elements
1
2
3
4
5

In the above example, set m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2.

Example 2

Let's see a simple example to exchange the content of two 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;
}

Output:

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

Example 3

Let's see a simple example to swap the contents of two 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;
}

Output:

set1:
	x
	y
	z
set2:
	a
	b
	c
	d

In the above example, another form of swap() function is used to swap the contents of two sets.

Example 4

Let's see a simple example:

#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;  
}  

Output:

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.





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf