#include #include #include #include #include #include using namespace std; // map // i.e., map to function // // Shows declaration using typedef and not using typedef. // Both work. // // --| functions to be called |-- void doit1(char *word[]) { printf("\tBegin doit1: word = %s, %s\n", word[0], word[1]); } void doit2(char *word[]) { printf("\tBegin doit2: word = %s, %s\n", word[0], word[1]); } void doit3(char *word[]) { printf("\tBegin doit3: word = %s, %s\n", word[0], word[1]); } typedef void (*F)(char *[]); struct ltstr { bool operator()(const char *s1, const char *s2) const { return strcmp(s1, s2) < 0; } }; int main (void) { map value; // doesn't use typedef // --| init table |-- value["doit1"] = &doit1; // note addresses value["doit2"] = &doit2; value["doit3"] = &doit3; printf("value.size() = %d\n", value.size()); printf("\n"); map::iterator p; // uses typedef int k = 0; char *command[3] = { "doit1", "doit2", "doit3" }; // fake data char *arg[3] = { "1", "2", "3" }; // . for (p = value.begin(); p != value.end(); p++) { char *word[2]; printf("Function for %s:\n", (*p).first); // show key word[0] = command[k]; // load up test args word[1] = arg[k]; // . ((*p).second)(word); // this is the actual function call // Note: table contains pairs // So, .first is the key and // .second is the target ++k; // on to next test command } printf("\n"); return 0; }