Created
July 9, 2018 22:36
-
-
Save saccadic/8378d392d57dd3104bef8d157727f511 to your computer and use it in GitHub Desktop.
大量の条件分岐を作成する時にswitchやifを使わない方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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