CHAPTER5-17.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER5-17.cpp
- #include <stdio.h>
- #include <stdlib.h>
- #include <set>
- #include <iostream>
- #include <functional>
- #include <algorithm>
- using namespace std;
- struct ltstr{ bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0;}};
- int main()
- {
- const int N = 6;
- const char* a[N] = {"isomer", "ephemeral", "prosaic", "nugatory", "artichoke", "serif"};
- const char* b[N] = {"flat", "this", "artichoke","frigate", "prosaic", "isomer"};
- set<const char*, ltstr> A(a, a + N);
- set<const char*, ltstr> B(b, b + N);
- set<const char*, ltstr> C;
- cout << "Set A: ";
- copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, " "));
- cout << endl;
- cout << "Set B: ";
- copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, " "));
- cout << endl;
- cout << "Union: ";
- set_union(A.begin(), A.end(), B.begin(), B.end(),ostream_iterator<const char*>(cout, " "),ltstr());
- cout << endl;
- cout << "Intersection: ";
- set_intersection(A.begin(), A.end(), B.begin(), B.end(),ostream_iterator<const char*>(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): ";
- copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));
- cout << endl;
- }