Consider the following code:
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> v = {1, 3, 4};
std::cout << v.size() << std::endl;
v.resize(0);
std::cout << v.size() << std::endl;
}
after compiling the output, as expected, is
3
0
Now I add a set difference.
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> x = {1, 3, 4};
std::list<int> y = {1, 2, 3, 5};
std::list<int> v;
x.sort();
y.sort();
std::cout << v.size() << std::endl;
auto it =
std::set_difference(x.begin(), x.end(), y.begin(), y.end(), v.begin());
std::cout << v.size() << std::endl;
v.resize(0);
std::cout << v.size() << std::endl;
}
and the output is
0
4
4
why does the resize work for the first example, but not in the second?