Advertisement
YauhenMardan

STL_Map

Apr 2nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include "Route.hpp"
  7.  
  8. #define II int,int
  9. #define IR int,Route
  10. #define IS int,string
  11. #define SR string,Route
  12. #define SS string,string
  13. #define SI string,int
  14.  
  15. using namespace std;
  16.  
  17. std::ostream& operator<< (std::ostream& os, Route& route);
  18. bool operator==(const Route& route1,const Route& route2);
  19.  
  20. void print_vec(vector<Route>);
  21.  
  22. void read_file(vector<Route>& _vec, string file);
  23.  
  24. int main(){
  25.     //--------------------------------------------------------------------------------------------------------------------------
  26. //1)
  27.     //Make vector
  28.     vector<Route> route_vec;
  29.     //Read file
  30. //2)
  31.     read_file(route_vec, "input.txt");
  32.     //Print vector
  33.     print_vec(route_vec);
  34. //3)
  35.     //Sort by №
  36.     sort(route_vec.begin(), route_vec.end(), [](Route route1, Route route2){return route1.get_bus_num()<route2.get_bus_num();});
  37.     //Print vector
  38.     print_vec(route_vec);
  39.     //--------------------------------------------------------------------------------------------------------------------------
  40.    
  41.     //--------------------------------------------------------------------------------------------------------------------------
  42. //5)
  43.         {
  44.             //Make map
  45.             map<IR> route_map;
  46.             for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  47.                 route_map.insert(route_map.begin(), pair<IR>(t_route.get_route_num(), t_route));
  48.             });
  49.             //Print routes num
  50.             cout<<"Current routes"<<endl;
  51.             for_each(route_map.begin(), route_map.end(), [](pair<IR> temp_route){cout<<temp_route.first<<" ";});
  52.             cout<<endl;
  53.         }
  54.     //--------------------------------------------------------------------------------------------------------------------------
  55. //6)
  56.         {
  57.             //Make map
  58.             multimap<IR> route_mul_map;
  59.             for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  60.                 route_mul_map.insert(route_mul_map.begin(), pair<IR>(t_route.get_route_num(), t_route));
  61.             });
  62.             {
  63.                 //Print route buses
  64.                 cout<<"Type route to print its buses"<<endl;
  65.                 int temp_route_num;
  66.                 cin>>temp_route_num;
  67.                 cout<<"Buses by "<<temp_route_num<<" route"<<endl;
  68.                 for_each(route_mul_map.lower_bound(temp_route_num), route_mul_map.upper_bound(temp_route_num), [](pair<IR> temp_route){
  69.                     cout<<temp_route.second.get_bus_num()<<" "<<temp_route.second.get_bus_brand()<<endl;
  70.                 });
  71.             }
  72. //7)
  73.             {
  74.                 //Print route buses
  75.                 cout<<"Type route to print its drivers"<<endl;
  76.                 int temp_route_num;
  77.                 cin>>temp_route_num;
  78.                 cout<<"Drivers by "<<temp_route_num<<" route"<<endl;
  79.                 for_each(route_mul_map.lower_bound(temp_route_num), route_mul_map.upper_bound(temp_route_num), [](pair<IR> temp_route){
  80.                     cout<<temp_route.second.get_driver_name()<<" ";
  81.                 });
  82.                 cout<<endl;
  83.             }
  84.         }
  85.     //--------------------------------------------------------------------------------------------------------------------------
  86. //8)
  87.         {
  88.             //Make map
  89.             multimap<SS> route_mul_map;
  90.             for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  91.                 route_mul_map.insert(route_mul_map.begin(), pair<SS>(t_route.get_bus_brand(), t_route.get_driver_name()));
  92.             });
  93.             //Print driver list
  94.             cout<<"Type bus brand to print its drivers"<<endl;
  95.             string temp_bus_brand;
  96.             cin>>temp_bus_brand;
  97.             cout<<"Drivers by "<<temp_bus_brand<<" brand"<<endl;
  98.             for_each(route_mul_map.lower_bound(temp_bus_brand), route_mul_map.upper_bound(temp_bus_brand), [](pair<SS> temp_route){
  99.                 cout<<temp_route.second<<" ";
  100.             });
  101.             cout<<endl;
  102.         }
  103.     //--------------------------------------------------------------------------------------------------------------------------
  104. //9)
  105.         {
  106.             //Make map
  107.             multimap<IR> route_mul_map;
  108.             for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  109.                 route_mul_map.insert(route_mul_map.begin(), pair<IR>(t_route.get_route_num(), t_route));
  110.             });
  111.             //Print map
  112.             cout<<"Routes map"<<endl;
  113.             for_each(route_mul_map.begin(), route_mul_map.end(), [](pair<IR> temp_route){cout<<temp_route.second<<endl;});
  114.             //Erase route
  115.             cout<<"Type route to delete it"<<endl;
  116.             int temp_route_num;
  117.             cin>>temp_route_num;
  118.             route_mul_map.erase(temp_route_num);
  119.             //Print map
  120.             cout<<"Routes map"<<endl;
  121.             for_each(route_mul_map.begin(), route_mul_map.end(), [](pair<IR> temp_route){cout<<temp_route.second<<endl;});
  122.             //Change bus
  123.         }
  124.     //--------------------------------------------------------------------------------------------------------------------------
  125. //10)
  126.         {
  127.             //Make map
  128.             map<SR> route_map;
  129.             for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  130.                 route_map.insert(route_map.begin(), pair<SR>(t_route.get_driver_name(), t_route));
  131.             });
  132.             //Change bus
  133.             cout<<"Type driver's name to change his/her bus"<<endl;
  134.             string temp_driver_name;
  135.             cin>>temp_driver_name;
  136.             cout<<"Type new bus num"<<endl;
  137.             string new_bus_num;
  138.             cin>>new_bus_num;
  139.             cout<<"Type new bus brand"<<endl;
  140.             string new_bus_brand;
  141.             cin>>new_bus_brand;
  142.             map<SR>::iterator it=route_map.find(temp_driver_name);
  143.             it->second.set_bus_num(new_bus_num);
  144.             it->second.set_bus_brand(new_bus_brand);
  145.             //Print map
  146.             cout<<"Routes map"<<endl;
  147.             for_each(route_map.begin(), route_map.end(), [](pair<SR> temp_route){cout<<temp_route.second<<endl;});
  148.         }
  149.     //--------------------------------------------------------------------------------------------------------------------------
  150. //11)
  151.     {
  152.         //Make map
  153.         map<SR> route_map;
  154.         for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  155.             route_map.insert(route_map.begin(), pair<SR>(t_route.get_bus_num(), t_route));
  156.         });
  157.         //Change driver
  158.         cout<<"Type bus num to change its driver"<<endl;
  159.         string temp_bus_num;
  160.         cin>>temp_bus_num;
  161.         cout<<"Type new driver's name"<<endl;
  162.         string new_driver_name;
  163.         cin>>new_driver_name;
  164.         map<SR>::iterator it=route_map.find(temp_bus_num);
  165.         it->second.set_driver_name(new_driver_name);
  166.         //Print map
  167.         cout<<"Routes map"<<endl;
  168.         for_each(route_map.begin(), route_map.end(), [](pair<SR> temp_route){cout<<temp_route.second<<endl;});
  169.     }
  170.     //--------------------------------------------------------------------------------------------------------------------------
  171.    
  172.     //--------------------------------------------------------------------------------------------------------------------------
  173. //12)
  174.     {
  175.         //Make map
  176.         multimap<IR> route_mul_map;
  177.         for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  178.             route_mul_map.insert(route_mul_map.begin(), pair<IR>(t_route.get_route_num(), t_route));
  179.         });
  180.         //Make map for each route
  181.         map<II> route_map;
  182.         for_each(route_mul_map.begin(), route_mul_map.end(), [&route_map](pair<IR> temp_route)->void{
  183.             route_map[temp_route.first]=0;
  184.         });
  185.         //Count buses for each route
  186.         for_each(route_map.begin(), route_map.end(), [&route_map, route_mul_map](pair<II> temp_route){
  187.             route_map[temp_route.first]=(int)route_mul_map.count(temp_route.first);
  188.         });
  189.         //Print map for each route
  190.         for_each(route_map.begin(), route_map.end(), [](pair<II> temp_route){
  191.             cout<<temp_route.second<<" bus(es) for "<<temp_route.first<<" route"<<endl;
  192.         });
  193.     }
  194.     //--------------------------------------------------------------------------------------------------------------------------
  195. //13)
  196.     {
  197.         //Make map
  198.         map<SI> route_map;
  199.         for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  200.             route_map.insert(route_map.begin(), pair<SI>(t_route.get_driver_name(), t_route.get_route_num()));
  201.         });
  202.         //Find route for bus driver
  203.         cout<<"Type driver's name"<<endl;
  204.         string temp_driver_name;
  205.         cin>>temp_driver_name;
  206.         cout<<"Its route is "<<route_map[temp_driver_name];
  207.     }
  208.     //--------------------------------------------------------------------------------------------------------------------------
  209. //14)
  210.     {
  211.         //Make multimap
  212.         multimap<IS> route_mul_map;
  213.         for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  214.             route_mul_map.insert(route_mul_map.begin(), pair<IS>(t_route.get_route_num(), t_route.get_bus_brand()));
  215.         });
  216.         //Make map
  217.         map<II> route_map;
  218.         //Find
  219.         for_each(route_mul_map.begin(), route_mul_map.end(), [route_mul_map, &route_map](pair<IS> temp_route){
  220.             vector<string> temp_vec;
  221.             int size1=0, size2=0;
  222.             for_each(route_mul_map.lower_bound(temp_route.first), route_mul_map.upper_bound(temp_route.first), [&temp_vec, &route_map](pair<IS> temp_temp_route){
  223.                 temp_vec.push_back(temp_temp_route.second);
  224.             });
  225.             sort(temp_vec.begin(), temp_vec.end());
  226.             size1=(int)temp_vec.size();
  227.             vector<string>::iterator it=unique(temp_vec.begin(), temp_vec.end());
  228.             temp_vec.resize(distance(temp_vec.begin(), it));
  229.             size2=(int)temp_vec.size();
  230.             if(size2==1){
  231.             route_map[temp_route.first]=size2;
  232.             }
  233.         });
  234.         //Print map
  235.         cout<<"Routes with the same brand: "<<endl;
  236.         for_each(route_map.begin(), route_map.end(), [](pair<II> temp_route){
  237.             cout<<temp_route.first<<" ";
  238.         });
  239.         cout<<endl;
  240.     }
  241.     //--------------------------------------------------------------------------------------------------------------------------
  242. //15)
  243.     {
  244.         //Make map
  245.         map<SR> route_map;
  246.         for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  247.             route_map.insert(route_map.begin(), pair<SR>(t_route.get_bus_brand(), t_route));
  248.         });
  249.         //Print bus_brands
  250.         cout<<"Brand list: ";
  251.         for_each(route_map.begin(), route_map.end(), [](pair<SR> temp_route){
  252.             cout<<temp_route.first<<" ";;
  253.         });
  254.         cout<<endl;
  255.     }
  256.     //--------------------------------------------------------------------------------------------------------------------------
  257. //16)
  258.     {
  259.         //Make multimap
  260.         multimap<IS> route_mul_map;
  261.         for_each(route_vec.begin(), route_vec.end(), [&route_mul_map](Route t_route)->void{
  262.             route_mul_map.insert(route_mul_map.begin(), pair<IS>(t_route.get_route_num(), t_route.get_bus_brand()));
  263.         });
  264.         //Make map
  265.         map<II> route_map;
  266.         for_each(route_vec.begin(), route_vec.end(), [&route_map](Route t_route)->void{
  267.             route_map.insert(route_map.begin(), pair<II>(t_route.get_route_num(), 0));
  268.         });
  269.        
  270.         for_each(route_mul_map.begin(), route_mul_map.end(), [route_mul_map, &route_map](pair<IS> temp_route){
  271.             vector<string> temp_vec;
  272.             int size1=0, size2=0;
  273.             for_each(route_mul_map.lower_bound(temp_route.first), route_mul_map.upper_bound(temp_route.first), [&temp_vec, &route_map](pair<IS> temp_temp_route){
  274.                 temp_vec.push_back(temp_temp_route.second);
  275.             });
  276.             sort(temp_vec.begin(), temp_vec.end());
  277.             size1=(int)temp_vec.size();
  278.             vector<string>::iterator it=unique(temp_vec.begin(), temp_vec.end());
  279.             temp_vec.resize(distance(temp_vec.begin(), it));
  280.             size2=(int)temp_vec.size();
  281.             route_map[temp_route.first]=size2;
  282.         });
  283.         //Print map
  284.         for_each(route_map.begin(), route_map.end(), [](pair<II> temp_route){
  285.             cout<<"Route: "<<temp_route.first<<" brands:"<<temp_route.second<<endl;
  286.         });
  287.     }
  288.     return 0;
  289. }
  290.  
  291. std::ostream& operator<< (std::ostream& os, Route& route) {
  292.     os<<route.to_string();
  293.     return os;
  294. }
  295. void print_vec(vector<Route> route_vec){
  296.     cout<<"Routes vector:"<<endl;
  297.     //Print
  298.     for_each(route_vec.begin(), route_vec.end(), [](Route temp_route){
  299.         cout<<temp_route<<endl;
  300.     });
  301. }
  302.  
  303. void read_file(vector<Route>& _vec, string file){
  304.     //Open file
  305.     ifstream ifile;
  306.     ifile.open("input.txt");
  307.     //Read input.txt
  308.     if(ifile.is_open()){
  309.         //Make temp values
  310.         string route_num="";
  311.         string driver_name="";
  312.         string bus_num_let="";
  313.         string bus_num_dig="";
  314.         string brand_bus="";
  315.        
  316.         string bus_num_let_temp="";
  317.         //Start reading
  318.         while(!ifile.eof()){
  319.             ifile>>route_num;
  320.             ifile>>driver_name;
  321.             ifile>>bus_num_dig;
  322.             ifile>>bus_num_let;
  323.             if(bus_num_let_temp==bus_num_let)
  324.                 break;
  325.             bus_num_let_temp=bus_num_let;
  326.             ifile>>brand_bus;
  327.             Route route_temp(atoi(route_num.c_str()), bus_num_dig+bus_num_let, driver_name, brand_bus);
  328.             _vec.push_back(route_temp);
  329.         }
  330.     }
  331.     //End reading
  332.     ifile.close();
  333. }
  334.  
  335. bool operator==(const Route& route1,const Route& route2){
  336.     if(route1.bus_num!=route2.bus_num)
  337.         return 0;
  338.     else{
  339.         if(route1.driver_name!=route2.driver_name)
  340.             return 0;
  341.         else{
  342.             if(route1.brand_bus!=route2.brand_bus)
  343.                 return 0;
  344.             else{
  345.                 if(route1.route_num!=route2.route_num)
  346.                     return 0;
  347.             }
  348.         }
  349.     }
  350.     return 1;
  351. }
  352.  
  353. //////////////////////////////////////////////////////////////////////////////////////////
  354. Route.hpp
  355. //////////////////////////////////////////////////////////////////////////////////////////
  356. #ifndef Route_hpp
  357. #define Route_hpp
  358.  
  359. #include <stdio.h>
  360. #include <string>
  361.  
  362. class Route{
  363. private:
  364.     int route_num;
  365.     std::string bus_num;
  366.     std::string driver_name;
  367.     std::string brand_bus;
  368. public:
  369.     Route(): route_num(0), bus_num(0), driver_name(""), brand_bus("") {}
  370.     Route(int r_num, std::string b_num, std::string d_name, std::string b_bus): route_num(r_num), bus_num(b_num), driver_name(d_name), brand_bus(b_bus) {}
  371.     Route(const Route& route): route_num(route.route_num), bus_num(route.bus_num), driver_name(route.driver_name), brand_bus(route.brand_bus) {}
  372.    
  373.     std::string to_string();
  374.    
  375.     int get_route_num();
  376.     std::string get_bus_num();
  377.     std::string get_driver_name();
  378.     std::string get_bus_brand();
  379.    
  380.     void set_driver_name(std::string new_driver_name);
  381.     void set_bus_num(std::string new_bus_num);
  382.     void set_bus_brand(std::string new_brand_bus);
  383.    
  384.     Route& operator=(const Route& route);
  385.    
  386.     friend std::ostream& operator<<(std::ostream&os, Route& route);
  387.     friend bool operator==(const Route& route1,const Route& route2);
  388.    
  389. };
  390.  
  391. #endif /* Route_hpp */
  392. //////////////////////////////////////////////////////////////////////////////////////////
  393. Route.cpp
  394. //////////////////////////////////////////////////////////////////////////////////////////
  395. #include "Route.hpp"
  396. #include <sstream>
  397.  
  398. std::string Route::to_string(){
  399.     std::ostringstream os;
  400.     os<<"RN:\t"<<route_num<<"\tDN\t"<<driver_name<<"\tBN\t"<<bus_num<<"\tBB\t"<<brand_bus;
  401.     return os.str();
  402. }
  403. std::string Route::get_bus_num(){
  404.     return bus_num;
  405. }
  406. std::string Route::get_bus_brand(){
  407.     return brand_bus;
  408. }
  409. int Route::get_route_num(){
  410.     return route_num;
  411. }
  412. std::string Route::get_driver_name(){
  413.     return driver_name;
  414. }
  415. void Route::set_driver_name(std::string new_driver_name){
  416.     driver_name=new_driver_name;
  417. }
  418. void Route::set_bus_num(std::string new_bus_num){
  419.     bus_num=new_bus_num;
  420. }
  421. void Route::set_bus_brand(std::string new_brand_bus){
  422.     brand_bus=new_brand_bus;
  423. }
  424. Route& Route::operator=(const Route& route){
  425.     bus_num=route.bus_num;
  426.     driver_name=route.driver_name;
  427.     brand_bus=route.brand_bus;
  428.     route_num=route.route_num;
  429.     return *this;
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement