// test_set.cpp just demo some features, not a full test // see test_set2.cpp for "inserter" to avoid extra vector #include #include // needed for set_ algorithms #include #include // needed for scratch container (should use reserve() ) using namespace std; typedef set my_set; // give my typename to a set of integers // this simplifies code for function prototypes, etc. void put(my_set S) // function to output a set, default order { cout << "S(" << S.size() << ")= "; // use begin() and end() to be safe for(my_set::const_iterator p=S.begin(); p!=S.end(); p++) cout << *p << " "; cout << endl; } int main() { my_set A; // construct an empty set my_set B; my_set C; // can't make bigger ??? my_set::const_iterator i; vector V(10); // need vector that can be resized vector::const_iterator p_end; // int * p_end; // replaces line above in g++ cout << "test_set running, S is " << (C.empty() ? "empty" : "has junk") << endl; cout << C.size() << "=size of C, max=" << C.max_size() << endl; A.insert(3); A.insert(5); A.insert(1); B.insert(1); B.insert(2); B.insert(3); cout << "initial A "; put(A); // begin() end() size() used in 'put()' cout << "initial B "; put(B); p_end = set_intersection(A.begin(), A.end(), B.begin(), B.end(), V.begin()); C.insert(V.begin(), p_end); // OK because C is empty cout << "intersection A B "; put(C); p_end = set_union(A.begin(), A.end(), B.begin(), B.end(), V.begin()); my_set D(V.begin(), p_end); // construct a new set cout << "union A B "; put(D); p_end = set_difference(A.begin(), A.end(), B.begin(), B.end(), V.begin()); C = my_set(V.begin(), p_end); // reuse object C but all new values cout << "difference A-B "; put(C); p_end = set_difference(B.begin(), B.end(), A.begin(), A.end(), V.begin()); C = my_set(V.begin(), p_end); cout << "difference B-A "; put(C); p_end = set_symmetric_difference(A.begin(), A.end(), B.begin(), B.end(), V.begin()); C = my_set(V.begin(), p_end); cout << "sym diff A B "; put(C); cout << "C = D "; C = D; put(C); cout << "first in set " << *D.begin() << endl; D.erase(D.begin()); cout << "erased first "; put(D); i = D.find(3); cout << "D.find(3) found " << *i << endl; i = D.find(4); cout << "D.find(4) " << (i==D.end()? "not found" : "4" /* *i */) << endl; cout << "test_set finished" << endl; return 0; } // Output of this program is: // test_set running, S is empty // 0=size of C, max=1073741823 // initial A S(3)= 1 3 5 // initial B S(3)= 1 2 3 // intersection A B S(2)= 1 3 // union A B S(4)= 1 2 3 5 // difference A-B S(1)= 5 // difference B-A S(1)= 2 // sym diff A B S(2)= 2 5 // C = D S(4)= 1 2 3 5 // first in set 1 // erased first S(3)= 2 3 5 // D.find(3) found 3 // D.find(4) not found // test_set finished