there are two
list<int> a (4,100);
list<int> b (4,200);
I use them as sets thus are sorted and uniqued:
a.sort();
a.unique();
b.sort();
b.unique();
Now the two list are like the following:
a: 100
b: 200
Now I calculate the union:
list <int> c(a.size() + b.size());
set_union(a.begin(), a.end(), b.begin(), b.end(), c.begin());
The result is fine but what I want is not to copy the two lists into another list. Because the list will be very big and do not contain ints for real.
set_union(a.begin(), a.end(), b.begin(), b.end(), a.begin());
Does not work bot i actually want the result to be in a without calulating a big copy and do
a = c;
aftterwards. An additional Problem is that if a = b = {100} then the result c is
{100, 0}!
Any ideas?
You can minimize copying with a
std::set_differencefollowed bylist::merge. Both are linear operations.The
blist isn’t modified and the temporaryclist is left empty.