say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them I have defined ==operator for the class1 objects class1a,class1b
say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them
Share
The stl algorithms provide several functions to perform set operations, notably calculating the set symmetric difference, which is what you need.
Here’s an example of use:
std::set_symmetric_differencetakes two range (i.e. two pairs of OutputIterators) and an InputIterator where it will put the result. It also returns an iterator to the end of the result range.EDIT
I just read your comments on your question. If you want the two original vectors to be modified, you can use
std::set_difference:Here, we put the result of the set difference v1 – v2 into v1. However, we can’t do the vice-versa since v1 is now modified. The solution is to calculate the intersection of v1 and v2, and then the difference with this intersection
std::set_intersection:I guess there are much more performant solutions, but this one is clear, and really convey your intents by using widely known stl algorithms.