// Phil Ottewell's STL Course - http://www.pottsoft.com/home/stl/stl.htmlx // // Example 7.2 © Phil Ottewell 1997 // // Purpose: // Demonstrate the use of a set. // ANSI C Headers #include // C++ STL Headers #include #include #include #ifdef _WIN32 using namespace std; # pragma warning(disable:4786) // We know basic_string generates long names :-) #endif struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; int main( int argc, char *argv[] ) { const char* a[] = { "Gray", "Pete", "Oggy", "Philip", "JAF", "Simon", "Nick", "Elliott", "Roy", "David", "Tony", "Nigel" }; const char* b[] = { "Sandy", "John", "Andrzej", "Rob", "Phil", "Happy", "Elliott", "Roy", "David", "Tony", "Nigel" }; set A(a, a + sizeof(a)/sizeof(a[0]) ); set B(b, b + sizeof(b)/sizeof(b[0]) ); set C; cout << "Set A: "; copy(A.begin(), A.end(), ostream_iterator(cout, " ") ); cout << endl; cout << "Set B: "; copy(B.begin(), B.end(), ostream_iterator(cout, " ") ); cout << endl; cout << "Union: "; set_union(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator(cout, " "), ltstr() ); cout << endl; cout << "Intersection: "; set_intersection(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator(cout, " "), ltstr() ); cout << endl; set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltstr() ); cout << "Set C (difference of A and B, i.e. in A but not B): "; copy(C.begin(), C.end(), ostream_iterator(cout, " ") ); cout << endl; set_symmetric_difference( A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltstr() ); cout << "Set C (symmetric difference of A and B," " i.e. in A OR B but not both): "; copy(C.begin(), C.end(), ostream_iterator(cout, " ") ); cout << endl; return( EXIT_SUCCESS ); }