How to merge multiple std::sets into a single std::set in C++?
If we want to merge multiple sets into a single set in C++, we must add all of the distinct components from the various sets we have been given. Std::set is a useful container offered by the standard library, which holds distinct and sorted elements. The standard library provides a number of methods that make it simple to combine multiple instances of std::set into a single std::set.
Example:
Set 1: {11, 14, 17}
Set 2: {12, 14, 15, 18}
Set 3: {13, 16, 18, 19, 20}
Output:
Final set :{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
Merging multiple sets in c++:
We can utilize the std::merge algorithm to merge two sets in C++. We can invoke the merge algorithm more than once to combine different sets in C++. Compilation requires the inclusion of std::merge since it is defined inside the <algorithm> header.
Example:
// C++ program to show the merging of multiple sets into a single set using merge algorithm.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
// driver code
int main()
{
// Creating multiple sets with different values
set<int> setA = { 10, 20, 30, 40 };
set<int> setB = { 30, 40, 50, 60 };
// Merging the sets by calling the merge algorithm
set<int> mergedSet;
merge(setA.begin(), setA.end(), setB.begin(),
setB.end(), inserter(mergedSet, mergedSet.begin()));
// Printing the merged set
cout << "Merged set: " << endl;
for (int element : mergedSet)
{
cout << element << " ";
}
return 0;
}
Output:
Merged set:
10 20 30 40 50 60
Example 2:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <vector>
// Function to merge multiple sets into a single set
template <typename T>
std::set<T> mergeSets(const std::vector<std::set<T>>& sets) {
std::set<T> mergedSet;
for (const auto& s : sets) {
std::merge(mergedSet.begin(), mergedSet.end(), s.begin(), s.end(), std::inserter(mergedSet, mergedSet.begin()));
}
return mergedSet;
}
int main() {
// Creating multiple sets with different values
std::set<int> set1 = {1, 2, 3, 4};
std::set<int> set2 = {3, 4, 5, 6};
std::set<int> set3 = {5, 6, 7, 8};
// Using the function to merge sets
std::vector<std::set<int>> allSets = {set1, set2, set3};
std::set<int> mergedSet = mergeSets(allSets);
// Printing the merged set
std::cout << "Merged set: " << std::endl;
for (int element : mergedSet) {
std::cout << element << " ";
}
return 0;
}
Output:
Merged set:
1 2 3 4 5 6 7 8
Explanation:
- In this example, an algorithm called mergeSets that takes a vector of sets and uses the std::merge to combine them into a single set.
- Three sets (set1, set2, and set3) with various values are created by the main function.
- After that, these sets are kept in a vector called allSets, from which the merged set is obtained by using the mergeSets function.
- Ultimately, the combined set is displayed on the console.
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Example 3:
#include <iostream>
#include <set>
#include <algorithm>
// Function to merge multiple sets into a single set using std::for_each and lambda
template <typename T>
std::set<T> mergeSets(const std::set<T>& set1, const std::set<T>& set2) {
std::set<T> mergedSet = set1;
std::for_each(set2.begin(), set2.end(), [&mergedSet](const T& element) {
mergedSet.insert(element);
});
return mergedSet;
}
int main() {
// Creating multiple sets with different values
std::set<int> setP = {1, 2, 3, 4};
std::set<int> setQ = {3, 4, 5, 6};
std::set<int> setR = {5, 6, 7, 8};
// Using the function to merge sets
std::set<int> mergedSet = mergeSets(mergeSets(setP, setQ), setR);
// Printing the merged set
std::cout << "Merged set: " << std::endl;
for (int element : mergedSet) {
std::cout << element << " ";
}
return 0;
}
Output:
Merged set:
1 2 3 4 5 6 7 8
Conclusion:
In conclusion, several techniques offered by the Standard Template Library (STL) can be used in C++ to combine multiple std::sets into a single std::set. Ensuring that each element in the final set is unique and sorted is usually the process. In common approaches, iterator-based insertion into the target set is combined with algorithms like std::merge or std::set_union. As an alternative, for simplicity, use the std::insert function. Developers can select the technique that most closely matches their needs and coding style. Efficiently combining unique elements from multiple sets into a single std::set is crucial, regardless of the method employed—merge, set_union, or insert.