fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. typedef map<string, void(*)()> Foo;
  7.  
  8. void call(const Foo& foo, const string& val, void(*defaultValue)() = NULL)
  9. {
  10. auto it = foo.find(val);
  11. if (it == foo.end())
  12. {
  13. if (defaultValue)
  14. {
  15. defaultValue();
  16. }
  17. }
  18. else
  19. {
  20. if (it->second)
  21. {
  22. it->second();
  23. }
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29. Foo foo;
  30. foo["asd"] = [](){ cout << "wybrales asd" << endl; };
  31. foo["xxx"] = [](){ cout << "wybrales xxx" << endl; };
  32. foo["yyy"] = [](){ cout << "wybrales yyy" << endl; };
  33.  
  34. string s;
  35. while (cin >> s)
  36. {
  37. call(foo, s, [](){ cout << "nie ma takiej opcji :(" << endl; });
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 3480KB
stdin
asd
xxx
yyy
dupa
stdout
wybrales asd
wybrales xxx
wybrales yyy
nie ma takiej opcji :(