TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset swap() function

C++ multiset swap() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset swap()

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

Syntax

void swap (multiset& x);

Parameter

x: multiset container to exchange the contents with.

Return value

None

Complexity

Constant.

Iterator validity

All references, iterators and pointers referring to elements in both multiset containers remain valid, but now are referring to elements in the other multiset container, and iterate on 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 multiset to another:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<int> m1 = {1,2,3,4,5,3};


   multiset<int> m2;

   m2.swap(m1);

   cout << "Multiset m2 contains following elements" << endl;

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

   return 0;
}

Output:

Multiset m2 contains following elements
1
2
3
3
4
5

In the above example, multiset 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 contents of two multisets:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  int myints[]={19,72,4,36,20,20};
  multiset<int> first (myints,myints+3);     // 4,19,72
  multiset<int> second (myints+3,myints+6);  // 20,20,36

  first.swap(second);

  cout << "first contains:";
  for (multiset<int>::iterator it=first.begin(); it!=first.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "second contains:";
  for (multiset<int>::iterator it=second.begin(); it!=second.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

Output:

first contains: 20 20 36
second contains: 4 19 72

Example 3

Let's see a simple example to swap the contents of two multisets:

#include<iostream>
#include<set>

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

Output:

multiset1:
	x
	y
	y
	z
multiset2:
	a
	b
	c
	c
	d

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

Example 4

Let's see a simple example:

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1, s2, s3;  
   multiset <int>::iterator s1_Iter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 10 );  
   s2.insert( 100 );  
   s2.insert( 200 );  
   s3.insert( 200 );  
  
   cout << "The original multiset 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, multiset 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, multiset s1 is:";  
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )  
      cout << " " << *s1_Iter;  
   cout   << "." << endl;  
}  

Output:

The original multiset s1 is: 10 10 20.
After swapping with s2, multiset s1 is: 100 200.
After swapping with s3, multiset s1 is: 200.
Next TopicC++ multiset




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