Use std::set_union and std::set_intersection, e.g.
int *c = std::set_union(setA, setA + a, setB, setB + b, setC)
int *c = std::set_intersection(setA, setA + a, setB, setB + b, setC)
where setC
is an output array of enough size; c
points to one past the last element of the constructed range.
If you want cardinalities, c - setC
is your answer.
I would recommend using something like std::vector
for representation, and iterators instead of arrays/pointers/lengths in algorithm calls.
Input ranges are assumed sorted; so is the output.
If you want to do it yourself, you'll find "possible implementations" at the links above.