C++ multiset Library - merge() Function



Description

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

Syntax

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

// Merge from another multiset
template <class C2>
void multiset_name.merge(multiset<Key, C2, Allocator>& source);
template <class C2>
void multiset_name.merge(multiset<Key, C2, Allocator>&& source);

Parameters

The merge() function accepts one parameter that represents the 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 multiset::merge() function is O(S·log(S+N)), where S is the size of the target multiset and N is the size of the source multiset.

Examples of multiset::merge() Function

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

Merging Two Multisets

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

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> nums1 = {10, 20, 30, 40, 50};
    multiset<int> nums2 = {25, 35, 45, 55};

    cout << "Multiset 1 before merge:";
    for (const auto &num : nums1)
        cout << " " << num;
    cout << endl;
    cout << "Multiset 2 before merge:";
    for (const auto &num : nums2)
        cout << " " << num;
    cout << endl;

    nums1.merge(nums2);
    cout << "\nMultiset 1 after merge:";
    for (const auto &num : nums1)
        cout << " " << num;
    cout << endl;
    cout << "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 −

Multiset 1 before merge: 10 20 30 40 50
Multiset 2 before merge: 25 35 45 55

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

Merging a Set into a Multiset

In this example, we are merging a set set1 into a multiset mset1 using the multiset::merge() function. The set becomes empty after merging with multiset.

#include <iostream>
#include <set>
using namespace std;
int main() {
    set<int> set1 = {15, 25, 35};
    multiset<int> mset1 = {10, 20, 20, 30};
    cout << "Multiset before merge:";
    for (const auto &num : mset1)
        cout << " " << num;
    cout << endl;
    cout << "Set before merge:";
    for (const auto &num : set1)
        cout << " " << num;
    cout << endl;
    mset1.merge(set1);
    cout << "\nMultiset after merge:";
    for (const auto &num : mset1)
        cout << " " << num;
    cout << endl;
    cout << "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 −

Multiset before merge: 10 20 20 30
Set before merge: 15 25 35

Multiset after merge: 10 15 20 20 25 30 35
Set after merge: (empty)

Merging Multisets of Different Data Types

The merge() function merges multisets having same data type. It will show a compilation error if 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 <set>
#include <string>
using namespace std;

int main() {
    multiset<int> numbers = {1, 2, 3};
    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);
      |     ~~~~~~~~~~~~~^~~~~~~
multiset.htm
Advertisements