c++ STL set difference

Steve picture Steve · Nov 12, 2008 · Viewed 51.7k times · Source

Does the C++ STL set data structure have a set difference operator?

Answer

PierreBdR picture PierreBdR · Nov 12, 2008

Yes there is, it is in <algorithm> and is called: std::set_difference. The usage is:

#include <algorithm>
#include <set>
#include <iterator>
// ...
std::set<int> s1, s2;
// Fill in s1 and s2 with values
std::set<int> result;
std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),
    std::inserter(result, result.end()));

In the end, the set result will contain the s1-s2.