How does one determine what the differences of 2 vectors are?
I have vector<int> v1 and vector<int> v2;
What I am looking for is a vector<int> vDifferences that contains only elements that are only in v1 or v2.
Is there a standard way to do this?
Here is the complete and correct answer. Before the
set_symmetric_differencealgorithm can be used, the source ranges must be ordered:It should be noted that sorting is an expensive operation (i.e., O(n log n) for common STL implementations). Especially for the case of one or both of the containers being very large (i.e., millions of integers or more), a different algorithm using hash tables may be preferable based on algorithmic complexity. Here is a high level description of this algorithm:
C++11 offers us some capability for such a solution by standardizing the
unordered_multisetcontainer. I also employed the new usage of theautokeyword for explicit initializations to make the following hash table based solution more concise:In order to determine which solution is better for a particular situation, profiling both algorithms would be a smart course of action. Although the hash table based solution is in O(n), it requires more code and does more work per duplicate found (i.e., hash table removals). It also (sadly) uses a custom differencing function rather than a standard STL algorithm.
It should be noted that both solutions present the differences in an order that is most likely quite different from the order that the elements appeared in the original containers. There is a way around this by using a variant of the hash table solution. A high level description follows (which only differs in Step 4 from the preceding solution):
In order to maintain original order, Step 4 has become more expensive than in the previous solution, especially if the number of items removed is high. This is because: