setops.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. ostream_iterator<string, char> out(cout, " ");
  16. cout << "Set A: ";
  17. copy(A.begin(), A.end(), out);
  18. cout << endl;
  19. cout << "Set B: ";
  20. copy(B.begin(), B.end(), out);
  21. cout << endl;
  22. cout << "Union of A and B:\n";
  23. set_union(A.begin(), A.end(), B.begin(), B.end(), out);
  24. cout << endl;
  25. cout << "Intersection of A and B:\n";
  26. set_intersection(A.begin(), A.end(), B.begin(), B.end(), out);
  27. cout << endl;
  28. cout << "Difference of A and B:\n";
  29. set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
  30. cout << endl;
  31. set<string> C;
  32. cout << "Set C:\n";
  33. set_union(A.begin(), A.end(), B.begin(), B.end(),
  34. insert_iterator<set<string> >(C, C.begin()));
  35. copy(C.begin(), C.end(), out);
  36. cout << endl;
  37. string s3("grungy");
  38. C.insert(s3);
  39. cout << "Set C after insertion:\n";
  40. copy(C.begin(), C.end(),out);
  41. cout << endl;
  42. cout << "Showing a range:\n";
  43. copy(C.lower_bound("ghost"),C.upper_bound("spook"), out);
  44. cout << endl;
  45. // cin.get();
  46. return 0;
  47. }