Advertisement
YauhenMardan

STL_Set

Apr 2nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <set>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. #define ONE_OR_TWO (rand()%2)
  9. #define _vec_BEGIN_END _vec.begin(), _vec.end()
  10. #define _FF_BEGIN_END first_fisher.begin(), first_fisher.end()
  11. #define _SF_BEGIN_END second_fisher.begin(), second_fisher.end()
  12. #define _TF_BEGIN_END third_fisher.begin(), third_fisher.end()
  13. #define _FU_BEGIN_END fish_union.begin(), fish_union.end()
  14. #define _FD_BEGIN_END all_fish.begin(), all_fish.end()
  15.  
  16. using namespace std;
  17.  
  18. void read_file_1(vector<string>& _vec, string file);
  19. void read_file_2(vector<string>& _vec, string file);
  20.  
  21. void print_set(set<string> _set);
  22.  
  23. int main() {
  24.     //-----------------------------------------------------------------------------------------------------
  25.     vector<string> _vec;
  26. //    read_file_1(_vec, "input.txt"); //no iter
  27.     read_file_2(_vec, "input.txt"); //stream iter
  28.     //Make set all_fish
  29.     set<string> all_fish;
  30.     copy(_vec.begin(), _vec.end(), inserter(all_fish, all_fish.begin()));
  31.     //Print set all_fish
  32.     ostream_iterator<string> o_iter(cout, " "); //как связываются??
  33.     cout<<"Рыбы в озере: ";
  34.     copy(all_fish.begin(), all_fish.end(), o_iter);
  35.     cout<<endl;
  36.     //-----------------------------------------------------------------------------------------------------
  37.     srand((int)time(NULL));
  38.     //Make set first_fisher
  39.     set<string> first_fisher;
  40.     for_each(_vec_BEGIN_END , [&first_fisher](string fish){ if(ONE_OR_TWO%2==0) first_fisher.insert(fish);});
  41.     //Print set first_fisher
  42.     cout<<"Первый рыбак: ";
  43.     print_set(first_fisher);
  44.     cout<<endl;
  45.     //-----------------------------------------------------------------------------------------------------
  46.     //Make set second_fisher
  47.     set<string> second_fisher;
  48.     for_each(_vec_BEGIN_END , [&second_fisher](string fish){ if(ONE_OR_TWO%2==0) second_fisher.insert(fish);});
  49.     //Print set second_fisher
  50.     cout<<"Второй рыбак: ";
  51.     print_set(second_fisher);
  52.     cout<<endl;
  53.     //-----------------------------------------------------------------------------------------------------
  54.     //Make set third_fisher
  55.     set<string> third_fisher;
  56.     for_each(_vec_BEGIN_END , [&third_fisher](string fish){ if(ONE_OR_TWO%2==0) third_fisher.insert(fish);});
  57.     //Print set third_fisher
  58.     cout<<"Третий рыбак: ";
  59.     print_set(third_fisher);
  60.     cout<<endl;
  61.     //----------------------------------------------------------------------------------------------------
  62.     //Make union
  63.     set<string> fish_union;
  64.     set_union(_FF_BEGIN_END, _SF_BEGIN_END, inserter(fish_union, fish_union.begin()));
  65.     set_union(_FU_BEGIN_END, _TF_BEGIN_END, inserter(fish_union, fish_union.begin()));
  66.     //Print union
  67.     cout<<"Рыбы, которые есть хотя бы у одного рыбака: ";
  68.     print_set(fish_union);
  69.     cout<<endl;
  70.     //----------------------------------------------------------------------------------------------------
  71.     //Make diff
  72.     set<string> fish_diff;
  73.     set_difference(_FD_BEGIN_END, _FU_BEGIN_END, inserter(fish_diff, fish_diff.begin()));
  74.     //Print diff
  75.     cout<<"Рыбы, которые есть в озере, но не в улове любого из рыбаков: ";
  76.     print_set(fish_diff);
  77.     cout<<endl;
  78.     return 0;
  79. }
  80.  
  81. void print_set(set<string> _set){
  82.     ostream_iterator<string> o_iter(cout, " ");
  83.     copy(_set.begin(), _set.end(), o_iter);
  84. }
  85.  
  86. void read_file_1(vector<string>& _vec, string file_name){
  87.     //Open file
  88.     fstream ifile;
  89.     ifile.open(file_name);
  90.     //Read input.txt
  91.     if(ifile.is_open()){
  92.         string buff;
  93.         while(!ifile.eof()){
  94.             ifile>>buff;
  95.             _vec.push_back(buff);
  96.         }
  97.     }
  98.     //End reading
  99.     ifile.close();
  100. }
  101.  
  102. void read_file_2(vector<string>& _vec, string file_name){
  103.     //Open file
  104.     fstream ifile;
  105.     ifile.open(file_name);
  106.     //Read input.txt
  107.     istream_iterator<string> initer(ifile), endinter;
  108.     copy(initer, endinter, back_inserter(_vec));
  109.     //End reading
  110.     ifile.close();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement