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++ std swap(multiset)

C++ Multiset swap(multiset) is a non-member function of multiset in C++. This is used to swap (or exchange) the contents of two multisets (i.e. x and y) but both the multisets must be of same type although sizes may differ.

Syntax

template <class T, class Compare, class Allocator>
void swap (multiset<T,Compare,Allocator>& x, multiset<T,Compare,Allocator>& y);

Parameter

x: First multiset object.

y: Second multiset object of the same type.

Return value

None

Complexity

Constant.

Iterator validity

All iterators, references and pointers referring to elements in both containers remain valid.

Note that the end iterators do not refer to elements and may be invalidated.

Data Races

Both containers x and y are modified.

No contained elements are accessed by the call.

Exception Safety

This function does not throw an exception.

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<char> m1 = {'a','b','b','d'};

   multiset<char> m2;

   swap(m1, m2);

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

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

   return 0;
}

Output:

Multiset contains following elements
a
b
b
d

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 ()
{
  multiset<int> multiset1,multiset2;

  multiset1= {100,200,100};

  multiset2 = {110, 220, 330};

  swap(multiset1,multiset2);

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

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

  return 0;
}

Output:

multiset1 contains:
110
220
330
multiset2 contains:
100
100
200

In the above example, contents of two multisets i.e. multiset1 and multiset2 are exchanged to each other.

Example 3

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

#include <iostream>
#include <set>

using namespace std;

 int main ()
{
  int myints[]={12,75,10,17,20,17};
  multiset<int> first (myints,myints+3);     
  multiset<int> second (myints+3,myints+6);  

  swap(first,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: 17 17 20
second contains: 10 12 75

Example 4

Let's see a simple example:

#include <iostream>
#include <string>
#include <set>

using namespace std;

void show(const char *msg, multiset<int> mp);

int main() {
  multiset<int> m1, m2;

  m1.insert(100);
  m1.insert(300);
  m1.insert(100);

  // Exchange the contents of m1 and m2.
  cout << "Exchange m1 and m2.\n";
  swap(m1,m2);
  show("Contents of m2: ", m2);
  show("Contents of m1: ", m1);

 // Clear m1.
  m1.clear();
  if(m1.empty()) cout << "m1 is now empty.";

  return 0;
}

// Display the contents of a multiset<string, int> by using an iterator.
void show(const char *msg, multiset<int> mp) {
  multiset<int>::iterator itr;

  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << *itr<< endl;
  cout << endl;
  
}

Output:

Exchange m1 and m2.
Contents of m2: 
  100
  100
  300

Contents of m1: 

m1 is now empty.

In the above example, contents of multiset m1 are swapped to multiset m2 and after swapping m1 multiset have been cleared.

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