C++ multiset Library - swap() Function



Description

The multiset::swap() function in C++ STL is used to exchange the content of one multiset with the content of another multiset of same type. It swaps all the elements, comparators, and allocators of one multiset with the other multiset.

Syntax

Following is the syntax of multiset::swap() function −

multiset1.swap(multiset2);

Parameters

The swap() function accepts one parameter that represents the multiset with which we want to swap the multiset.

Return value

The swap() function does not return any value. It has a return type of void.

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of multiset::swap() function is O(1).

Examples of multiset::swap() Function

The following examples demonstrate the usage of multiset::swap() function in multiset:

Swapping Two Multisets

Below is an example to swap the contents of two multisets set1 and set2 using the multiset::swap() function −

#include <iostream>
#include <set>
using namespace std;
void printEle(const string& name, const multiset<int> &ms){
   cout << name << ": ";
   for (auto it = ms.begin(); it != ms.end(); ++it)
      cout << *it << " ";
   cout << endl;
}
int main(){
   multiset<int> set1 = {10, 20, 30, 40, 50};
   multiset<int> set2 = {100, 200, 300};
   cout << "Before swap:" << endl;
   printEle("set1", set1);
   printEle("set2", set2);
   set1.swap(set2);
   cout << "\nAfter swap:" << endl;
   printEle("set1", set1);
   printEle("set2", set2);
   return 0;
}

The output of the above code is given below −

Before swap:
set1: 10 20 30 40 50 
set2: 100 200 300 

After swap:
set1: 100 200 300 
set2: 10 20 30 40 50

Swapping Empty and Non-Empty Multisets

You can use swap() function to swap an empty multiset with a non empty multiset. The empty multiset becomes a non empty multiset and non empty multiset becomes an empty multiset after swapping.

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> numbers = {1, 2, 3, 4, 5};
    multiset<int> empty_set;
    cout << "Before swap:" << endl;
    cout << "numbers size: " << numbers.size() << endl;
    cout << "empty_set size: " << empty_set.size() << endl;
    numbers.swap(empty_set);
    cout << "\nAfter swap:" << endl;
    cout << "numbers size: " << numbers.size() << endl;
    cout << "empty_set size: " << empty_set.size() << endl;
    cout << "empty_set contents: ";
    for (int n : empty_set)
        cout << n << " ";
    cout << endl;
    return 0;
}

The output of the code is given below −

Before swap:
numbers size: 5
empty_set size: 0

After swap:
numbers size: 0
empty_set size: 5
empty_set contents: 1 2 3 4 5

Effect of multiset::swap() on Iterators

The following example demonstrates that after swapping the iterators remain valid but they point to swapped elements now. For example: if itr1 was pointing to first element of multiset1 then after swapping, itr1 will be pointing to first element of multiset2.

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> set1 = {10, 20, 30};
    multiset<int> set2 = {40, 50, 60};
    auto it1 = set1.begin();
    auto it2 = set2.begin();
    cout << "Before swap:" << endl;
    cout << "it1 points to: " << *it1 << " (in set1)" << endl;
    cout << "it2 points to: " << *it2 << " (in set2)" << endl;
    set1.swap(set2);
    cout << "\nAfter swap:" << endl;
    cout << "it1 points to: " << *it1 << " (now in set2)" << endl;
    cout << "it2 points to: " << *it2 << " (now in set1)" << endl;
    cout << "\nCurrent contents:" << endl;
    cout << "set1: ";
    for (int n : set1)
        cout << n << " ";
    cout << endl;
    cout << "set2: ";
    for (int n : set2)
        cout << n << " ";
    cout << endl;
    return 0;
}

The output of the code is given below −

Before swap:
it1 points to: 10 (in set1)
it2 points to: 40 (in set2)

After swap:
it1 points to: 10 (now in set2)
it2 points to: 40 (now in set1)

Current contents:
set1: 40 50 60 
set2: 10 20 30

Swapping Multisets of Custom Structures

In this example, we have swapped value of two multisets that store user defined structures −

#include <iostream>
#include <set>
using namespace std;

struct Product {
    string name;
    int price;
    bool operator<(const Product &p) const
    {
        return price < p.price;
    }
};

void printMultiset(const multiset<Product> &products)
{
    int count = 0, n = products.size();
    for (const auto &p : products) {
        cout << p.name << " " << p.price;
        if (++count < n) 
            cout << ", ";
    }
    cout << endl;
}

int main() {
    multiset<Product> shopA = {{"Pen", 10}, {"Book", 30}};
    multiset<Product> shopB = {{"Pencil", 5}, {"Notebook", 25}};

    cout << "Shop A before swap:\n";
    printMultiset(shopA);
    cout << "Shop B before swap:\n";
    printMultiset(shopB);

    shopA.swap(shopB);

    cout << "\nShop A after swap:\n";
    printMultiset(shopA);
    cout << "Shop B after swap:\n";
    printMultiset(shopB);
}

The output of the above code is given below −

Shop A before swap:
Pen 10, Book 30
Shop B before swap:
Pencil 5, Notebook 25

Shop A after swap:
Pencil 5, Notebook 25
Shop B after swap:
Pen 10, Book 30
multiset.htm
Advertisements