How to find the intersection of sets

I have N numbers of sets, say, integers. Now I want a function that finds me at the intersection of these sets. For example, for the following

Set1 = { A, D, E, F, G, L } Set2 = { N, K, E, G, B, C } Set3 = { K, P, Q, E, F, G } Set4 = { Z, Y, C, G, F, E } 

Since E and G are in each set, I have to get { E, G } as output. The easiest way to do this. I know that it is not very difficult to write your own code, but perhaps there is already an STL or any other library function that I am interested in for this.

+4
source share
2 answers

Two possible solutions that I can think of

  • Store your sets in vectors. Sort vectors with std::sort and compute the intersection of the set with std::set_intersection
  • Store your sets in std::set , which will sort the items anyway and use std::set_intersection
+3
source

See std::set_intersection . (As mentioned in the comments, you probably want an intersection, not a union.)

+1
source

All Articles