fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <memory>
  5. #include <cctype>
  6.  
  7. class ICommand
  8. {
  9. public:
  10. virtual ~ICommand(){}
  11. virtual void Execute() = 0;
  12. };
  13.  
  14. class ProcessSingleStatusCommand : public ICommand
  15. {
  16. public:
  17. void Execute()
  18. {
  19. std::cout << "ProcessSingleStatusCommand" << std::endl;
  20. }
  21. };
  22.  
  23. class ProcessMarriedStatusCommand : public ICommand
  24. {
  25. public:
  26. void Execute()
  27. {
  28. std::cout << "ProcessMarriedStatusCommand" << std::endl;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34. typedef std::map<char, std::unique_ptr<ICommand>> TCommandMap;
  35. TCommandMap commandMap;
  36.  
  37. commandMap.insert(std::make_pair('s', std::unique_ptr<ICommand>(new ProcessSingleStatusCommand())));
  38. commandMap.insert(std::make_pair('m', std::unique_ptr<ICommand>(new ProcessMarriedStatusCommand())));
  39.  
  40. // Assume we've captured inputs 's' & 'M'
  41. std::vector<char> inputs {'s', 'M'};
  42.  
  43. for (const auto& input : inputs)
  44. {
  45. auto it = commandMap.find(tolower(input));
  46.  
  47. if (it != commandMap.end())
  48. {
  49. it->second->Execute();
  50. }
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 3416KB
stdin
s
stdout
ProcessSingleStatusCommand
ProcessMarriedStatusCommand