问题
I have a couple questions about how to use C++ sets (std::set)
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)
Can C++ sets be used as keys in a map?
回答1:
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.
回答2:
As for your first question, have a look at
- set_union
- set_intersection
- set_difference.
回答3:
Anything can be used as a key in a map as long as you provide a class or function that can compare them. Here is an example.
来源:https://stackoverflow.com/questions/1736017/getting-union-intersection-or-difference-of-sets-in-c