s1 and s2 are sets (Python or C ++ set std :: set)
To add s2 elements to s1 (set the union) you can do
Python: s1.update(s2) C++: s1.insert(s2.begin(), s2.end());
To remove s2 elements from s1 (set the difference) you can do
Python: s1.difference_update(s2)
What is the C ++ equivalent? The code
s1.erase(s2.begin(), s2.end());
does not work, s1.erase () requires iterators from s1.Code
std::set<T> s3; std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(s3, s3.end()); s1.swap(s3);
works, but seems too complicated, at least compared to Python.
Is there an easier way?
user763305
source share