fork download
  1. //перевести затем в шаблон
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <iterator>
  6. #include <algorithm>
  7.  
  8. namespace Integer_operations {
  9. using std::set_union;
  10. using std::set_intersection;
  11. using std::insert_iterator;
  12. using std::copy;
  13. using std::ostream_iterator;
  14. using std::cout;
  15.  
  16. //template <typename T>
  17. class Intset {
  18. typedef std::vector<int> Array;
  19.  
  20. const Array& InputArray1, InputArray2;
  21. Array Union, Intersection;
  22.  
  23. bool Union_status;
  24. bool Intersection_status;
  25.  
  26. void make_Union();
  27. void make_Intersection();
  28.  
  29. //deleted constructors
  30. Intset(const Intset&);
  31. Intset& operator=(const Intset&);
  32.  
  33. public:
  34.  
  35. Intset(Array& argInputArray1, Array& argInputArray2)
  36. : InputArray1(argInputArray1), InputArray2(argInputArray2),
  37. Union_status(false), Intersection_status(false)
  38. {
  39. }
  40.  
  41. ~Intset()
  42. {
  43. }
  44.  
  45. Intset& print_Union();
  46. Intset& print_Intersection();
  47. };
  48. }
  49.  
  50. // MAKE FOR CONSTANT SOURCES
  51.  
  52. //#include "intset.hpp"
  53.  
  54. namespace Integer_operations {
  55.  
  56. //template <typename T>
  57. void Intset::make_Union()
  58. {
  59. insert_iterator<Array> Iter_insert_to_Union(Union, Union.begin());
  60. set_union(InputArray1.begin(), InputArray1.end(), InputArray2.begin(), InputArray2.end(), Iter_insert_to_Union);
  61.  
  62. Union_status = true;
  63. }
  64. //template <typename T>
  65. inline Intset& Intset::print_Union()
  66. {
  67. if (Union_status) {
  68. cout << ".:Union:.\n";
  69. ostream_iterator<int, char> Iter_cout(cout, ", ");
  70. copy(Union.begin(), Union.end(), Iter_cout);
  71. } else {
  72. make_Union();
  73. print_Union();
  74. }
  75.  
  76. return *this;
  77. }
  78.  
  79. //template <typename T>
  80. void Intset::make_Intersection()
  81. {
  82. insert_iterator<Array> Iter_insert_to_Isect(Intersection, Intersection.begin());
  83. set_intersection(InputArray1.begin(), InputArray1.end(), InputArray2.begin(), InputArray2.end(), Iter_insert_to_Isect);
  84.  
  85. Intersection_status = true;
  86. }
  87.  
  88. //template <typename T>
  89. inline Intset& Intset::print_Intersection()
  90. {
  91. if (Intersection_status) {
  92. std::cout << ".:Intersection:.\n";
  93. ostream_iterator<int, char> Iter_cout(cout, ", ");
  94. copy(Intersection.begin(), Intersection.end(), Iter_cout);
  95. } else {
  96. make_Intersection();
  97. print_Intersection();
  98. }
  99.  
  100. return *this;
  101. }
  102. }
  103.  
  104. //#include "intset.hpp"
  105.  
  106. int main()
  107. {
  108. using namespace Integer_operations;
  109. const int Xs = 11, Ys = 14;
  110. int X[Xs] = {4, 1, 7, 43, 98, 1, 3, 75, 9, 0, 32},
  111. Y[Ys] = {7, 98, 1, 34, 9, 642, 34, 9, 3, 423, 2, 53, 2, 6};
  112. std::vector<int> V1(X, X+Xs), V2(Y, Y+Ys);
  113.  
  114.  
  115. Intset I(V1, V2);
  116. I.print_Intersection().print_Union();
  117. return 0;
  118.  
  119. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
.:Intersection:.
7, 98, 1, .:Union:.
4, 1, 7, 43, 98, 1, 3, 34, 9, 75, 9, 0, 32, 642, 34, 9, 3, 423, 2, 53, 2, 6,