You should use std::set_difference : http://en.cppreference.com/w/cpp/algorithm/set_difference
First you will need to sort your vectors , since set_difference works with sorted ranges. That is, if they are not already sorted (as in your use case).
std::sort(vector1.begin(), vector1.end()); std::sort(vector2.begin(), vector2.end());
Then you call it like this:
std::vector<int> difference; std::set_difference( vector1.begin(), vector1.end(), vector2.begin(), vector2.end(), std::back_inserter( difference ) );
This will add to the difference those elements that are in vector1 that are not found in vector2 .
source share