fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void smithJonesAnalysis (int data){cout << "Smith";}
  8. void pineappleMangoAnalysis (int data){cout << "Pineapple";}
  9.  
  10. class Analyzer
  11. {
  12. enum class AnalysisMethod {SmithJones, PineappleMango};
  13. std::map<AnalysisMethod, std::function<void(int)>> m_analysis_map;
  14.  
  15. AnalysisMethod stringToMethod (std::string method)
  16. {
  17. std::transform(method.begin(), method.end(), method.begin(), ::tolower);
  18. if (method == "smith-jones method")
  19. return AnalysisMethod::SmithJones;
  20. if (method == "pineapple-mango method")
  21. return AnalysisMethod::PineappleMango;
  22.  
  23. throw std::runtime_error("Invalid analysis method");
  24. }
  25. public:
  26. Analyzer()
  27. {
  28. m_analysis_map[AnalysisMethod::SmithJones] = smithJonesAnalysis;
  29. m_analysis_map[AnalysisMethod::PineappleMango] = pineappleMangoAnalysis;
  30. }
  31. void operator() (std::string method, int data)
  32. {
  33. AnalysisMethod am = stringToMethod(method);
  34. m_analysis_map[am](data);
  35. }
  36.  
  37. private:
  38.  
  39.  
  40.  
  41. };
  42.  
  43. int main() {
  44. Analyzer a;
  45. a("Smith-Jones Method", 0);
  46. a("Pineapple-Mango Method", 0);
  47. return 0;
  48. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
SmithPineapple