C++ unordered_multiset::swap() Function



Description

The unordered_multiset::swap() function in C++ STL is used to exchange the content of one unordered_multiset with the content of another unordered_multiset of same type. It swaps all the elements, hash functions, key equality predicates, and allocators of one unordered_multiset with the other unordered_multiset.

Syntax

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

unordered_multiset1.swap(unordered_multiset2);

Parameters

The swap() function accepts one parameter that represents the unordered_multiset with which we want to swap the unordered_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 unordered_multiset::swap() function is O(1).

Examples of unordered_multiset::swap() Function

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

Swapping Two Unordered_multisets

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

#include <iostream>
#include <unordered_set>
using namespace std;
void printEle(const string& name, const unordered_multiset<int> &ums){
    cout << name << ": ";
    for (auto it = ums.begin(); it != ums.end(); ++it)
        cout << *it << " ";
    cout << endl;
}
int main() {
    unordered_multiset<int> set1 = {10, 20, 30, 40, 50};
    unordered_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: 50 40 30 20 10 
set2: 300 200 100 

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

Swapping Empty and Non-Empty Unordered_multisets

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

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_multiset<int> numbers = {1, 2, 3, 4, 5};
    unordered_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: 5 4 3 2 1

Effect of unordered_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 an element of unordered_multiset1 then after swapping, itr1 will be pointing to that same element which is now in unordered_multiset2.

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_multiset<int> set1 = {10, 20, 30};
    unordered_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: 30 (in set1)
it2 points to: 60 (in set2)

After swap:
it1 points to: 30 (now in set2)
it2 points to: 60 (now in set1)

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

Swapping Unordered_multisets of Custom Structures

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

#include <iostream>
#include <unordered_set>
using namespace std;
struct Product {
    string name;
    int price;
    bool operator==(const Product &p) const {
        return name == p.name && price == p.price;
    }
};
struct ProductHash {
    size_t operator()(const Product &p) const {
        return hash<string>()(p.name) ^ hash<int>()(p.price);
    }
};
void printUnorderedMultiset(const unordered_multiset<Product, ProductHash> &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() {
    unordered_multiset<Product, ProductHash> shopA = {{"Pen", 10}, {"Book", 30}};
    unordered_multiset<Product, ProductHash> shopB = {{"Pencil", 5}, {"Notebook", 25}};
    cout << "Shop A before swap:\n";
    printUnorderedMultiset(shopA);
    cout << "Shop B before swap:\n";
    printUnorderedMultiset(shopB);
    shopA.swap(shopB);
    cout << "\nShop A after swap:\n";
    printUnorderedMultiset(shopA);
    cout << "Shop B after swap:\n";
    printUnorderedMultiset(shopB);
}

The output of the above code is given below −

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

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

Difference Between multiset::swap and unordered_multiset::swap

The differences between multiset::swap and unordered_multiset::swap functions are given below −

multiset::swap unordered_multiset::swap
The multiset::swap() function works on a multiset. The unordered_multiset::swap() function works on an unordered multiset.
It swaps elements along with comparators and allocators. It swaps elements along with hash functions, key equality predicates, and allocators.
After swapping, the elements remain in sorted order in both multisets. After swapping, the elements remain in unordered arrangement in both unordered multisets.
Advertisements