Skip to content

Instantly share code, notes, and snippets.

@jgreitemann
Created June 22, 2016 08:39
Show Gist options
  • Save jgreitemann/c892ad8191893d4293eb135912ca6000 to your computer and use it in GitHub Desktop.
Save jgreitemann/c892ad8191893d4293eb135912ca6000 to your computer and use it in GitHub Desktop.
Ways to map cmd line args to function pointers
#include <iostream>
#include <cstdlib>
double f_one(double x) {
return x;
}
double f_two(double x) {
return x*x;
}
double f_three(double x) {
return x*x*x;
}
void tabulate(double (*func_ptr)(double)) {
for (double x = 0.; x < 5; x += .5)
std::cout << x << "\t" << func_ptr(x) << std::endl;
}
int main (int argc, const char* argv[]) {
double (*fps[])(double) = {&f_one, &f_two, &f_three};
if (argc != 2)
return 1;
int funcOpt = std::atoi(argv[1]);
// check that funcOpt-1 is in range!
tabulate(fps[funcOpt - 1]);
return 0;
}

Hier sind drei Varianten dein Problem zu lösen. Zunächst ist es einfacher und saubrer, wenn du die Zeile

window = new TF1("name", fp, 0, 1, 1);

nur einmal schreibst. fp ist in dem Fall ein function pointer mit Typ double (*)(double) (also eine Funktion, die double zurückgibt und ein double Argument annimmt).

switch.cpp ist dann im wesentlichen dein Ausgangsprogramm, aber mit den if..else if durch das switch statement ersetzt. Beachte, dass ich darin aber nur den function pointer zuweise und dann erst danach tabulate aufrufe (was bei dir der TF1-Zeile entspräche).

array.cpp erstellt ein Array all der function pointers und nutzt dann das numerische Argument als Index. Die function pointers sind jeweils nur 64 bit groß, daher ist das effizient. Ein Array von Fenstern wäre vermutlich nicht so wünschenswert. ^^

map.cpp macht dann schlussendlich das, was du in Python mit dicts machen würdest. dict heißt in C++ map.

Aufrufe:

./switch 1
./switch 2
./switch 3
./array 1
./array 2
./array 3
./map one
./map two
./map three
#include <iostream>
#include <string>
#include <map>
double f_one(double x) {
return x;
}
double f_two(double x) {
return x*x;
}
double f_three(double x) {
return x*x*x;
}
void tabulate(double (*func_ptr)(double)) {
for (double x = 0.; x < 5; x += .5)
std::cout << x << "\t" << func_ptr(x) << std::endl;
}
int main (int argc, const char* argv[]) {
std::map<std::string, double (*)(double)> arg_map;
arg_map["one"] = &f_one;
arg_map["two"] = &f_two;
arg_map["three"] = &f_three;
if (argc != 2)
return 1;
std::string arg_str(argv[1]);
// validate arg_str
tabulate(arg_map[arg_str]);
return 0;
}
#include <iostream>
#include <cstdlib>
double f_one(double x) {
return x;
}
double f_two(double x) {
return x*x;
}
double f_three(double x) {
return x*x*x;
}
void tabulate(double (*func_ptr)(double)) {
for (double x = 0.; x < 5; x += .5)
std::cout << x << "\t" << func_ptr(x) << std::endl;
}
int main (int argc, const char* argv[]) {
double (*fp)(double);
if (argc != 2)
return 1;
int funcOpt = std::atoi(argv[1]);
switch (funcOpt) {
case 1:
fp = &f_one;
break;
case 2:
fp = &f_two;
break;
case 3:
fp = &f_three;
break;
default:
return 1;
}
tabulate(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment