Advertisement
ortem_kats

salibek_lab2

Jan 26th, 2020
2,633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include <functional>
  7.  
  8. using namespace std;
  9.  
  10. class Product{
  11.     public : Product(string cfc, string ctc, string c, string n){
  12.         country_from_code = cfc;
  13.         country_to_code = ctc;
  14.         code = c;
  15.         num = n;
  16.     }
  17.    
  18.     public : string country_from_code;
  19.     public : string country_to_code;
  20.     public : string code;
  21.     public : string num;
  22.    
  23.     public : bool check(string abbr){
  24.         return *country_from_code.begin() == *abbr.begin() &&
  25.                 *country_to_code.begin() == *(abbr.begin()+1) &&
  26.                  *code.begin() == *(abbr.begin()+2);
  27.     }
  28.    
  29.     public : string get_str_code(){
  30.         return this->country_from_code + " " + this->country_to_code + " " + this->code;
  31.     }
  32. };
  33.  
  34. int main()
  35. {
  36.     vector<Product> products = {
  37.         *(new Product("FR742", "BEL1254", "TR4587", "1236547")),
  38.         *(new Product("RU1254", "FR4567", "GT12454", "1236548")),
  39.         *(new Product("FR654", "GER4526", "LK1245", "1236549")),
  40.         *(new Product("FR742", "BEL1254", "TR4587", "1236550")),
  41.         *(new Product("FIN1254", "BUL252", "TW1247", "1236551"))
  42.     };
  43.    
  44.     map<string, int> m;
  45.    
  46.     for (auto& p : products)
  47.         if (p.check("FBT") && m.find(p.get_str_code()) == m.end())
  48.             m.insert(make_pair(p.get_str_code(), 1));
  49.         else
  50.             (m.find(p.get_str_code()))->second++;
  51.            
  52.     typedef function<bool(pair<string, int>, pair<string, int>)> Comparator;
  53.  
  54.     Comparator compFunctor =
  55.             [](pair<string, int> elem1, pair<string, int> elem2)
  56.             {
  57.                 return elem1.second > elem2.second;
  58.             };
  59.  
  60.     set<pair<string, int>, Comparator> s(
  61.             m.begin(), m.end(), compFunctor);
  62.            
  63.     for (auto& p : s)
  64.         cout << p.first << " " << p.second << endl;
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement