CPP/STL/SetDifference
From ProgrammingExamples
SetDifference.cpp
#include <iostream> #include <list> #include <algorithm> #include <string> #include <iterator> //prints any collection that supports begin and end iterator template<class Coll> void print(const Coll c){ using std::cout; using std::endl; typename Coll::const_iterator itr = c.begin(); cout << "{ "; while(itr != c.end()) { cout << *itr++ << " "; } cout << "}"; } int main() { using namespace std; string s1 = "abce"; string s2 = "abde"; list<char> a = list<char>(s1.begin(),s1.end()); list<char> b = list<char>(s2.begin(),s2.end()); list<char> res; //need to sort before call to set_difference b.size(); a.sort(); std::set_difference(a.begin(),a.end(),b.begin(),b.end(),std::back_insert_iterator<list<char> >(res)); print(a); cout << " - "; print(b); cout << " = "; print(res); cout << endl; return 0; }