Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Union, Intersection, or Difference of Sets in C++

I have a couple questions about how to use C++ sets (std::set)

  1. Is there a way to get the union, intersection, or difference of two C++ sets? (It's pretty easy to write my own functionto do that but I wanted to know if there was a built in function for it)

  2. Can C++ sets be used as keys in a map?

like image 395
Alex319 Avatar asked Nov 14 '09 23:11

Alex319


People also ask

How are unions and intersections of sets different?

What is the difference between union and intersection? A union of sets produces a new set containing each element present in the original sets. An intersection of sets produces a new set that contains only the elements that the original sets have in common.

Is set difference the same as intersection?

Intersection: Elements two sets have in common. Union: All the elements from both sets. Difference: Elements present on one set, but not on the other.


1 Answers

Use the set_difference(), set_union(), set_intersection() and set_symmetric_difference() functions.

Sets and maps support any key type that can compare. By default this means the type has operator<() defined, but you can provide your own comparator. C++ sets don't have operator<() defined and therefore can't be used as keys unless you provide your own comparator.

like image 180
wilhelmtell Avatar answered Sep 16 '22 22:09

wilhelmtell