When using algorithms with output iterator such as std::set_difference
, or std::transform
, is there any simple way of overwriting the original vector?
vector<int> v { 0, 1, 2, 3 };
vector<int> v2{ 1, 3 };
std::set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v.begin());
v = { 0, 2, 2, 3}
In this specific case I can probably do something like:
auto it = std::set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v.begin());
v.resize(std::distance(v.begin(), it));
v = { 0, 2 }
But it looks like a hassle, it's not very readable, and it only works if a destination <= to source in size.
Is there a modular way to go about this?