b = {'H', 'G', 'F', 'E', 'D'}
In C++, the union of these two sets would be:
The elements of set b is joined to the elements of set a without having twice any element that was in both sets. Any such element appears once in the new set. The new set is sorted in ascending order with default settings.
Before C++20, the algorithm library had to be used in a rather complicated way to have the union of two sets. To this end, the algorithm library has the function set_union() in different overloaded forms with varying arguments. These overloaded functions are still in use today, but set_union() will not be addressed in this article.
The set class in C++20 has two overloaded merge() member functions to obtain the union of two sets. The syntaxes might look complicated, but they are very easy to use. The merge member functions will be used in this article to show how to obtain the union of two sets.
template<class C2> void merge(set<Key, C2, Allocator>& source)
This member function creates a union of two sets. It returns void. It is the set of interest that employs the member function, merge(). The other set merges with the set of interest. The identifier of the other set is the argument to the merge member function.
The argument looks complicated, but it is not. The argument is:
This is the template for set. It begins with the reserved word, set. Remember, that set single elements are called keys. So, the first template parameter is for the key type. It can be char, float, double, string, etc. The second template parameter is for the compare class object. If omitted, the result is that the set will be sorted ascending, internally. The third parameter in the angle brackets is for memory allocation of the set elements. If omitted, the default allocator is chosen. Source stands for the identifier of the other set (or incoming set). So, all that complex argument parameter in parentheses of the syntax is replaced by the identifier of the other set in the program.
When creating the set of interest or the other set, these parameters should be taken into consideration. For typical programming, only the key has to be taken into consideration.
Let the following set be the set of interest:
Let the following set be the other set (or incoming set).
The following program merges set b into set ‘a’. The new set ‘a’ is the union of the old set ‘a’ and set ‘b’. The values in set b that are not in set ‘a’ are moved to set ‘a’.
#include <set>
using namespace std;
int main()
{
set a = {'E', 'D', 'C', 'B', 'A'};
set b = {'H', 'G', 'F', 'E', 'D'};
a.merge(b);
for (set::iterator iter = a.begin(); iter != a.end(); iter++)
cout << *iter << ", ";
cout << endl;
return 0;
}
The output is:
Note that the union set has been arranged in ascending order using the default Compare class.
template<class C2> void merge(set<Key, C2, Allocator>&& source)
This is the other overloaded member function to merge two sets. This member function creates a union of two sets. It returns void. It is the set of interest that employs the member function, merge(). The other set merges with the set of interest. The identifier of the other set is the argument to the merge() function. This time, the identifier is a rvalue reference identifier.
The argument looks complicated, but it is not. The argument is:
This is the template for set. It begins with the reserved word, set. Remember that set single elements are called keys. So, the first template parameter is for the key type. It can be char, float, double, string, etc. The second template parameter is for the compare class object. If omitted, the result is that the set will be sorted ascending, internally. The third parameter in the angle brackets is for memory allocation of the set elements. If omitted, the default allocator is chosen. Source in this case stands for the rvalue reference identifier of the other set (or incoming set). So, all that complex argument parameter in parentheses of the syntax is replaced by the rvalue reference identifier of the other set in the program. The double amperes AND, &&, in this case, means rvalue reference. This is where this function differs from the previous.
When creating the set of interest or the other set, these parameters should be taken into consideration. For typical programming, only the key has to be taken into consideration.
Let the following set be the set of interest:
Let the following set literal, be the other set (or incoming set).
The following program merges set b into set ‘a’. The new set ‘a’ is the union of the old set ‘a’ and set ‘b’. The values in set b that are not in set ‘a’ are moved to set ‘a’.
#include <set>
using namespace std;
int main()
{
set<char> a = {'E', 'D', 'C', 'B', 'A'};
set<char>&& b = {'H', 'G', 'F', 'E', 'D'};
a.merge(b);
for (set<char>::iterator iter = a.begin(); iter != a.end(); iter++)
cout << *iter << ", ";
cout << endl;
return 0;
}
The output is:
Note that the union set has been arranged in ascending order using the default Compare class.
Conclusion
Having the union of two sets is also merging both sets. The predefined set class in C++ has two overloaded member functions for this purpose. The incoming set (or the other set) merges with the set of interest. The set of interest employs the merge() member function. The identifier of the incoming set is the argument of the merge() function. Only elements that are not in the set of interest are moved from the incoming set to the set of interest. The set class is in the set library and has to be included into the program.