Skip to content

Instantly share code, notes, and snippets.

@saccadic
Created July 9, 2018 22:36
Show Gist options
  • Save saccadic/8378d392d57dd3104bef8d157727f511 to your computer and use it in GitHub Desktop.
Save saccadic/8378d392d57dd3104bef8d157727f511 to your computer and use it in GitHub Desktop.
大量の条件分岐を作成する時にswitchやifを使わない方法
#include <map>
#include <string>
#include <iostream>
void funcA() { std::cout << "invoke func A\n" ; }
void funcB() { std::cout << "invoke func B\n" ; }
int main(void) {
 std::map< std::string, void(*)(void) > funcs ;
 funcs[ "あいうえお" ] = funcA ;
 funcs[ "かきこくけこ" ] = funcB ;
 std::string str = "あいうえお" ;
 void(*f)(void) = 0;
 f = funcs[str];
 if ( !f )
  // default: の処理
 else
  f();
 return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment