Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- #include <map>
- #include<functional>
- #include <set>
- #include <vector>
- class Date
- {
- private:
- std::tuple<int,int,int> dateTuple;
- public:
- Date(const int &day, const int &month, const int &year)
- {
- this->dateTuple=std::make_tuple(day, month, year);
- }
- std::tuple<int,int,int> getTuple()
- {
- return this->dateTuple;
- }
- bool isLess(Date *date)
- {
- if(std::get<2>(this->dateTuple)<std::get<2>(date->getTuple())) //compare year
- return true;
- if(std::get<1>(this->dateTuple)<std::get<1>(date->getTuple())) //compare month
- return true;
- if(std::get<0>(this->dateTuple)<std::get<0>(date->getTuple())) //compare day
- return true;
- return false;
- }
- };
- class Calendar
- {
- private:
- std::map<Date*, std::vector<std::string>> calendar;
- // std::vector<std::map<Date*, std::string>> calendar;
- public:
- void mapEvent(const std::string &eventName, Date* date)
- {
- std::vector<std::string> v;
- v.push_back(eventName);
- //calendar[date]=v;
- calendar.insert(std::pair<Date*, std::vector<std::string>>(date, v));
- }
- std::string getEvent(Date *date)
- {
- std::string str="";
- for(auto& cal : calendar)
- {
- if(cal.first==date)
- {
- for(auto& evName : cal.second)
- {
- str.append(evName);
- }
- }
- }
- return str;
- }
- void printSortedEvents()
- {
- // Declaring the type of Predicate that accepts 2 pairs and return a bool
- typedef std::function<bool(std::pair<Date*, std::string>, std::pair<Date*, std::string>)> Comparator;
- // Defining a lambda function to compare two pairs. It will compare two pairs using second field
- Comparator compFunctor =
- [](std::pair<Date*, std::string> elem1,std::pair<Date*, std::string> elem2)
- {
- return elem1.first->isLess(elem2.first);
- };
- // Declaring a set that will store the pairs using above comparision logic
- std::set<std::pair<Date*, std::string>, Comparator> dates(
- calendar.begin(), calendar.end(), compFunctor);
- // Iterate over a set using range base for loop
- // It will display the items in sorted order of values
- for (std::pair<Date*, std::string> element : dates)
- std::cout << element.second << std::endl;
- }
- };
- int main()
- {
- Date d(24, 5, 2019);
- Date d1(25, 5, 2019);
- Date d2(25, 5, 2020);
- Date d3(25, 5, 2019);
- Date d4(26, 5, 2019);
- Calendar calendar;
- calendar.mapEvent("first event", &d);
- calendar.mapEvent("last event", &d2);
- calendar.mapEvent("not first event", &d1);
- calendar.mapEvent("not first event2", &d1);
- calendar.printSortedEvents();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement