appg04.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // setops.cpp -- some set operations
  2. #include <iostream>
  3. #include <string>
  4. #include <set>
  5. #include <algorithm>
  6. #include <iterator>
  7. int main()
  8. {
  9. using namespace std;
  10. const int N = 6;
  11. string s1[N] = {"buffoon", "thinkers", "for", "heavy", "can", "for"};
  12. string s2[N] = {"metal", "any", "food", "elegant", "deliver","for"};
  13. set<string> A(s1, s1 + N);
  14. set<string> B(s2, s2 + N);
  15. A.insert("buffalo");
  16. ostream_iterator<string, char> out(cout, " ");
  17. cout << "Set A: ";
  18. copy(A.begin(), A.end(), out);
  19. cout << endl;
  20. cout << "Set B: ";
  21. copy(B.begin(), B.end(), out);
  22. cout << endl;
  23. cout << "Union of A and B:\n";
  24. set_union(A.begin(), A.end(), B.begin(), B.end(), out);
  25. cout << endl;
  26. cout << "Intersection of A and B:\n";
  27. set_intersection(A.begin(), A.end(), B.begin(), B.end(), out);
  28. cout << endl;
  29. cout << "Difference of A and B:\n";
  30. set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
  31. cout << endl;
  32. set<string> C;
  33. cout << "Set C:\n";
  34. set_union(A.begin(), A.end(), B.begin(), B.end(),
  35. insert_iterator<set<string> >(C, C.begin()));
  36. copy(C.begin(), C.end(), out);
  37. cout << endl;
  38. string s3("grungy");
  39. C.insert(s3);
  40. cout << "Set C after insertion:\n";
  41. copy(C.begin(), C.end(),out);
  42. cout << endl;
  43. cout << "Showing a range:\n";
  44. copy(C.lower_bound("ghost"),C.upper_bound("spook"), out);
  45. cout << endl;
  46. cin.get();
  47. cin.get();
  48. return 0;
  49. }