SHARE
TWEET

samp

hacityler May 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <tuple>
  3. #include <map>
  4. #include<functional>
  5. #include <set>
  6. #include <vector>
  7.  
  8. class Date
  9. {
  10. private:
  11.  
  12.     std::tuple<int,int,int> dateTuple;
  13.  
  14. public:
  15.  
  16.     Date(const int &day, const int &month, const int &year)
  17.     {
  18.         this->dateTuple=std::make_tuple(day, month, year);
  19.     }
  20.  
  21.     std::tuple<int,int,int> getTuple()
  22.     {
  23.         return this->dateTuple;
  24.     }
  25.  
  26.     bool isLess(Date *date)
  27.     {
  28.  
  29.         if(std::get<2>(this->dateTuple)<std::get<2>(date->getTuple())) //compare year
  30.             return true;
  31.  
  32.         if(std::get<1>(this->dateTuple)<std::get<1>(date->getTuple())) //compare month
  33.             return true;
  34.  
  35.         if(std::get<0>(this->dateTuple)<std::get<0>(date->getTuple()))  //compare day
  36.             return true;
  37.  
  38.         return false;
  39.     }
  40. };
  41.  
  42. class   Calendar
  43. {
  44. private:
  45.  
  46.     std::map<Date*, std::vector<std::string>> calendar;
  47.  
  48. //    std::vector<std::map<Date*, std::string>> calendar;
  49.  
  50. public:
  51.  
  52.     void mapEvent(const std::string &eventName, Date* date)
  53.     {
  54.  
  55.         std::vector<std::string> v;
  56.         v.push_back(eventName);
  57.         //calendar[date]=v;
  58.  
  59.         calendar.insert(std::pair<Date*, std::vector<std::string>>(date, v));
  60.     }
  61.  
  62.     std::string getEvent(Date *date)
  63.     {
  64.         std::string str="";
  65.  
  66.         for(auto& cal : calendar)
  67.         {
  68.             if(cal.first==date)
  69.             {
  70.                 for(auto& evName : cal.second)
  71.                 {
  72.                     str.append(evName);
  73.                 }
  74.             }
  75.         }
  76.         return str;
  77.     }
  78.  
  79.  
  80. void printSortedEvents()
  81. {
  82.     // Declaring the type of Predicate that accepts 2 pairs and return a bool
  83.     typedef std::function<bool(std::pair<Date*, std::string>, std::pair<Date*, std::string>)> Comparator;
  84.  
  85.     // Defining a lambda function to compare two pairs. It will compare two pairs using second field
  86.     Comparator compFunctor =
  87.         [](std::pair<Date*, std::string> elem1,std::pair<Date*, std::string> elem2)
  88.     {
  89.         return elem1.first->isLess(elem2.first);
  90.     };
  91.  
  92.     // Declaring a set that will store the pairs using above comparision logic
  93.     std::set<std::pair<Date*, std::string>, Comparator> dates(
  94.         calendar.begin(), calendar.end(), compFunctor);
  95.  
  96.     // Iterate over a set using range base for loop
  97.     // It will display the items in sorted order of values
  98.     for (std::pair<Date*, std::string> element : dates)
  99.         std::cout << element.second << std::endl;
  100. }
  101. };
  102.  
  103. int main()
  104. {
  105.     Date d(24, 5, 2019);
  106.     Date d1(25, 5, 2019);
  107.     Date d2(25, 5, 2020);
  108.     Date d3(25, 5, 2019);
  109.     Date d4(26, 5, 2019);
  110.  
  111.     Calendar calendar;
  112.  
  113.     calendar.mapEvent("first event", &d);
  114.     calendar.mapEvent("last event", &d2);
  115.     calendar.mapEvent("not first event", &d1);
  116.     calendar.mapEvent("not first event2", &d1);
  117.  
  118.     calendar.printSortedEvents();
  119.  
  120.     return 0;
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top