D
Dave
Hello all,
I have three vector<int> v1, v2 and v3. I want to copy everything from v1
to v3, except for the items that are also in v2. This sounds tailor-made
for set_difference() but for one catch: I need the order preserved, which
set_difference() does not do.
As it stands now, I copy v1 to v3, manually loop over v2 and, for each
element in v2, remove the first occurrence (if any) of that element from v3.
I also considered remove_if() in conjunction with a predicate that would
check for the presence of the item in v2, but a state change of the
predicate is required, and that's not allowed.
Here's an example of the required behavior:
v1: 6 5 4 7 6 3 8 2 1
v2: 3 6 2
v3: 5 4 7 6 8 1
What is the lexically shortest way to accomplish this? Can anybody beat a
manual loop with calls to find() and erase() such as I have done?
Thanks,
Dave
I have three vector<int> v1, v2 and v3. I want to copy everything from v1
to v3, except for the items that are also in v2. This sounds tailor-made
for set_difference() but for one catch: I need the order preserved, which
set_difference() does not do.
As it stands now, I copy v1 to v3, manually loop over v2 and, for each
element in v2, remove the first occurrence (if any) of that element from v3.
I also considered remove_if() in conjunction with a predicate that would
check for the presence of the item in v2, but a state change of the
predicate is required, and that's not allowed.
Here's an example of the required behavior:
v1: 6 5 4 7 6 3 8 2 1
v2: 3 6 2
v3: 5 4 7 6 8 1
What is the lexically shortest way to accomplish this? Can anybody beat a
manual loop with calls to find() and erase() such as I have done?
Thanks,
Dave