fork(2) download
  1. #include <functional>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void foo();
  9. void foo2();
  10.  
  11. int main(){
  12. map<string, function<void()>> funcs{{"foo", foo},{"foo2", foo2}};
  13.  
  14. string s;
  15. cin >> s;
  16.  
  17. /*Take the input from cin and turn it into a function call, or some other
  18.   form of runnable code, like so*/
  19.  
  20. auto findIt = funcs.find(s);
  21. if (findIt != end(funcs)) (findIt->second)();
  22. }
  23.  
  24. void foo(){
  25. cout << "Foo works, yay :D";
  26. }
  27.  
  28. void foo2(){
  29. cout << "Foo2 works, yay :D";
  30. }
  31.  
Success #stdin #stdout 0s 3468KB
stdin
foo
stdout
Foo works, yay :D