Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <set>
- #include <algorithm>
- #include <string>
- #define ONE_OR_TWO (rand()%2)
- #define _vec_BEGIN_END _vec.begin(), _vec.end()
- #define _FF_BEGIN_END first_fisher.begin(), first_fisher.end()
- #define _SF_BEGIN_END second_fisher.begin(), second_fisher.end()
- #define _TF_BEGIN_END third_fisher.begin(), third_fisher.end()
- #define _FU_BEGIN_END fish_union.begin(), fish_union.end()
- #define _FD_BEGIN_END all_fish.begin(), all_fish.end()
- using namespace std;
- void read_file_1(vector<string>& _vec, string file);
- void read_file_2(vector<string>& _vec, string file);
- void print_set(set<string> _set);
- int main() {
- //-----------------------------------------------------------------------------------------------------
- vector<string> _vec;
- // read_file_1(_vec, "input.txt"); //no iter
- read_file_2(_vec, "input.txt"); //stream iter
- //Make set all_fish
- set<string> all_fish;
- copy(_vec.begin(), _vec.end(), inserter(all_fish, all_fish.begin()));
- //Print set all_fish
- ostream_iterator<string> o_iter(cout, " "); //как связываются??
- cout<<"Рыбы в озере: ";
- copy(all_fish.begin(), all_fish.end(), o_iter);
- cout<<endl;
- //-----------------------------------------------------------------------------------------------------
- srand((int)time(NULL));
- //Make set first_fisher
- set<string> first_fisher;
- for_each(_vec_BEGIN_END , [&first_fisher](string fish){ if(ONE_OR_TWO%2==0) first_fisher.insert(fish);});
- //Print set first_fisher
- cout<<"Первый рыбак: ";
- print_set(first_fisher);
- cout<<endl;
- //-----------------------------------------------------------------------------------------------------
- //Make set second_fisher
- set<string> second_fisher;
- for_each(_vec_BEGIN_END , [&second_fisher](string fish){ if(ONE_OR_TWO%2==0) second_fisher.insert(fish);});
- //Print set second_fisher
- cout<<"Второй рыбак: ";
- print_set(second_fisher);
- cout<<endl;
- //-----------------------------------------------------------------------------------------------------
- //Make set third_fisher
- set<string> third_fisher;
- for_each(_vec_BEGIN_END , [&third_fisher](string fish){ if(ONE_OR_TWO%2==0) third_fisher.insert(fish);});
- //Print set third_fisher
- cout<<"Третий рыбак: ";
- print_set(third_fisher);
- cout<<endl;
- //----------------------------------------------------------------------------------------------------
- //Make union
- set<string> fish_union;
- set_union(_FF_BEGIN_END, _SF_BEGIN_END, inserter(fish_union, fish_union.begin()));
- set_union(_FU_BEGIN_END, _TF_BEGIN_END, inserter(fish_union, fish_union.begin()));
- //Print union
- cout<<"Рыбы, которые есть хотя бы у одного рыбака: ";
- print_set(fish_union);
- cout<<endl;
- //----------------------------------------------------------------------------------------------------
- //Make diff
- set<string> fish_diff;
- set_difference(_FD_BEGIN_END, _FU_BEGIN_END, inserter(fish_diff, fish_diff.begin()));
- //Print diff
- cout<<"Рыбы, которые есть в озере, но не в улове любого из рыбаков: ";
- print_set(fish_diff);
- cout<<endl;
- return 0;
- }
- void print_set(set<string> _set){
- ostream_iterator<string> o_iter(cout, " ");
- copy(_set.begin(), _set.end(), o_iter);
- }
- void read_file_1(vector<string>& _vec, string file_name){
- //Open file
- fstream ifile;
- ifile.open(file_name);
- //Read input.txt
- if(ifile.is_open()){
- string buff;
- while(!ifile.eof()){
- ifile>>buff;
- _vec.push_back(buff);
- }
- }
- //End reading
- ifile.close();
- }
- void read_file_2(vector<string>& _vec, string file_name){
- //Open file
- fstream ifile;
- ifile.open(file_name);
- //Read input.txt
- istream_iterator<string> initer(ifile), endinter;
- copy(initer, endinter, back_inserter(_vec));
- //End reading
- ifile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement