Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple sets elements in a single set

Tags:

c++

set

stl

I would like to know if there is any std library or boost tool to easily merge the contents of multiple sets into a single one.

In my case I have some sets of ints which I would like to merge.

like image 986
codeJack Avatar asked Aug 17 '11 07:08

codeJack


People also ask

How can you combine two sets into one?

Press Ctrl+click to highlight two sets, and right-click either set to display a shortcut menu that allows you to Create Combined Set. Drag the pill for the set you want to combine to the left of the other set on the Rows shelf, and select Create Combined Set.

Can you combine sets in Java?

Since a Set does not contain duplicate entries, you can therefore combine the two by: newStringSet. addAll(oldStringSet); It does not matter if you add things twice, the set will only contain the element once...

Can we add two sets in C++?

With C++17, you can use merge function of set directly. This is better, when you want the set2 elements extracted & inserted into set1 as part of merging.

How to merge multiple sets in Java 8?

1. Using Java 8 We can use streams in Java 8 and above to merge multiple sets by obtaining a stream consisting of all elements from every set using the static factory method Stream.of () and accumulating all elements into a new set using a Collector. We can also accumulate all elements using forEach (), as shown below:

How to merge two sets into one set container in C++?

This post will discuss how to merge elements of the two given sets into a new set container in C++. Since all elements in a set are distinct, the common elements in both input sequences are discarded in the destination set. 1. Using std::set::insert function

How to merge two sets using addall function in a class?

A class named Demo contains a function named ‘set_merge’ that uses the ‘addAll’ function to merge the two sets that are passed as parameters to the function. In the main function, two sets are defined and elements are added into it using the ‘addAll’ function.

How do you combine two sets in a union?

Using union () method to combine sets in python Union () method is used to combine two sets in python and returns a combined set. We can perform a union of as many sets as we want. If two sets contain common items, then their union will only have one copy of that item because all elements in a set are unique.


2 Answers

You can do something like:

std::set<int> s1; std::set<int> s2; // fill your sets s1.insert(s2.begin(), s2.end()); 
like image 119
Nicola Musatti Avatar answered Sep 18 '22 11:09

Nicola Musatti


Looks like you are asking for std::set_union.

Example:

#include <set> #include <algorithm>  std::set<int> s1;  std::set<int> s2;  std::set<int> s3;  // Fill s1 and s2   std::set_union(std::begin(s1), std::end(s1),                std::begin(s2), std::end(s2),                                  std::inserter(s3, std::begin(s3)));  // s3 now contains the union of s1 and s2 
like image 41
Antonio Pérez Avatar answered Sep 21 '22 11:09

Antonio Pérez