C++ set intersection
#include <iostream> #include <set> #include <algorithm> using namespace std; void DumpClass(set<string> *myset) { set<string>::iterator iter = myset->begin(); while (iter != myset->end()) {//from w w w.ja v a 2 s . c o m cout << *iter << endl; iter++; } } int main() { set<string> set1; set<string> set2; set1.insert("Z"); set1.insert("M"); set1.insert("V"); set1.insert("U"); set1.insert("C"); set2.insert("V"); set2.insert("U"); set2.insert("R"); set2.insert("O"); set<string> Union; set<string> Intersection; insert_iterator<set<string>> IntersectIterate(Intersection, Intersection.begin()); insert_iterator<set<string>> UnionIterate(Union, Union.begin()); set_intersection(set1.begin(),set1.end(),set2.begin(), set2.end(),IntersectIterate); cout << "===Intersection===" << endl; DumpClass(&Intersection); set_union(set1.begin(),set1.end(),set2.begin(), set2.end(),UnionIterate); cout << endl << "===Union===" << endl; DumpClass(&Union); return 0; }