- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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);
| ~~~~~~~~~~~~~^~~~~~~