fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class entity{
  9. static const char* colorArray[];
  10. size_t _index;
  11. int _time;
  12. public:
  13. entity(int time) : _index(0U), _time(time) {}
  14. void AddRed() {_index |= 4U;}
  15. void AddYellow() {_index |= 2U;}
  16. void AddBlue() {_index |= 1U;}
  17. auto GetInitializedTime() const {return _time;}
  18. auto Result() const {return colorArray[_index];}
  19. };
  20.  
  21. const char* entity::colorArray[] = {"black", "blue", "yellow", "green", "red", "purple", "orange", "white"};
  22.  
  23. int main() {
  24. map<int, function<void(entity&)>> events = {{3, mem_fn(&entity::AddRed)}, {9, mem_fn(&entity::AddBlue)}, {11, mem_fn(&entity::AddYellow)}};
  25. vector<entity> Entity = {entity(1), entity(8), entity(13)};
  26.  
  27. for(auto& i : Entity) {
  28. for(auto j = events.upper_bound(i.GetInitializedTime()); j != events.end(); ++j) {
  29. j->second(i);
  30. }
  31. cout << i.Result() << endl;
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
white
green
black