C++ unordered_multiset::merge() Function



Description

The unordered_multiset::merge() function in C++ STL is used to merge elements from one unordered_set/unordered_multiset into another unordered_multiset. It extracts all elements of the source unordered_set or unordered_multiset and inserts them into the target unordered_multiset. After merging, the source unordered_set or unordered_multiset becomes an empty container.

Syntax

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

// Merge from another unordered_multiset
template <class H2, class P2>
void unordered_multiset_name.merge(unordered_multiset<Key, H2, P2, Allocator>& source);
template <class H2, class P2>
void unordered_multiset_name.merge(unordered_multiset<Key, H2, P2, Allocator>&& source);

Parameters

The merge() function accepts one parameter that represents the unordered_multiset from which we want to extract the elements.

Return value

The merge() function has void return type. It does not return any value.

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of unordered_multiset::merge() function is O(N) on average, where N is the size of the source unordered_multiset. In the worst case, it is O(N·(S+N)) where S is the size of the target unordered_multiset.

Examples of unordered_multiset::merge() Function

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

Merging Two Unordered_Multisets

Below is an example to merge two unordered_multisets: nums1 and nums2 into nums1 using the unordered_multiset::merge() function −

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_multiset<int> nums1 = {10, 20, 30, 40, 50};
    unordered_multiset<int> nums2 = {25, 35, 45, 55};
    cout << "Unordered_Multiset 1 before merge:";
    for (const auto &num : nums1)
        cout << " " << num;
    cout << endl;
    cout << "Unordered_Multiset 2 before merge:";
    for (const auto &num : nums2)
        cout << " " << num;
    cout << endl;
    nums1.merge(nums2);
    cout << "\nUnordered_Multiset 1 after merge:";
    for (const auto &num : nums1)
        cout << " " << num;
    cout << endl;
    cout << "Unordered_Multiset 2 after merge:";
    if (nums2.empty())
        cout << " (empty)";
    else
        for (const auto &num : nums2)
            cout << " " << num;
    cout << endl;
    return 0;
}

The output of the above code is given below −

Unordered_Multiset 1 before merge: 50 40 30 20 10
Unordered_Multiset 2 before merge: 55 45 35 25
Unordered_Multiset 1 after merge: 25 35 45 55 50 40 30 20 10
Unordered_Multiset 2 after merge: (empty)

Merging an Unordered_Set into an Unordered_Multiset

In this example, we are merging an unordered_set set1 into an unordered_multiset mset1 using the unordered_multiset::merge() function. The unordered_set becomes empty after merging with unordered_multiset.

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
    unordered_set<int> set1 = {15, 25, 35};
    unordered_multiset<int> mset1 = {10, 20, 20, 30};
    cout << "Unordered_Multiset before merge:";
    for (const auto &num : mset1)
        cout << " " << num;
    cout << endl;
    cout << "Unordered_Set before merge:";
    for (const auto &num : set1)
        cout << " " << num;
    cout << endl;
    mset1.merge(set1);
    cout << "\nUnordered_Multiset after merge:";
    for (const auto &num : mset1)
        cout << " " << num;
    cout << endl;
    cout << "Unordered_Set after merge:";
    if (set1.empty())
        cout << " (empty)";
    else
        for (const auto &num : set1)
            cout << " " << num;
    cout << endl;
    return 0;
}

The output of the above code is given below −

Unordered_Multiset before merge: 30 20 20 10
Unordered_Set before merge: 35 25 15
Unordered_Multiset after merge: 15 25 35 30 20 20 10
Unordered_Set after merge: (empty)

Merging Unordered_Multisets of Different Data Types

The merge() function merges unordered_multisets having same data type. It will show a compilation error if unordered_multisets of different data types are merged. Here is an example of merging a string data type with int that shows a compilation error −

#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
    unordered_multiset<int> numbers = {1, 2, 3};
    unordered_multiset<string> words = {"apple", "banana"};
    // It will show a compilation error
    numbers.merge(words);
}

The output of the above code is given below −

mismatched types 'int' and 'std::__cxx11::basic_string<char>'
   12 |     numbers.merge(words);
      |     ~~~~~~~~~~~~~^~~~~~~
Run the above example codes in the compilers that supports C++17 and above.

Difference Between multiset::merge and unordered_multiset::merge

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

multiset::merge unordered_multiset::merge
The multiset::merge() function works on a multiset. The unordered_multiset::merge() function works on an unordered multiset.
It merges elements from a sorted container where elements are maintained in either ascending or descending order. It merges elements from a hash based container where elements are organized using hash values without any specific order.
After merging, the new multiset maintains sorted order of all elements. After merging, the new unordered_multiset has no fixed order of elements.
Time complexity is O(S·log(S+N)), where S is the size of target and N is the size of source. Time complexity is O(N) on average, where N is the size of source. Worst case is O(N·(S+N)).
Advertisements