Ok, this may be really noobish question, but i am wishing there is something i dont know yet.
I go through a file, and check which string each line has, depending on the string value i execute a different function for it (or functions).
This is how i do it now:
Edit: I need to use variables outside the if-else-if range inside the if’s, updated code:
string s1 = "used";
string s2 = "in";
string s3 = "functions";
if(str == "something"){
something = process(s1, s2);
}else if(str == "something else"){
something = process(s2, s3);
}else if(str == "something more"){
something = process(s1, s3);
something = process(s1, s2);
}else if(str == "something again"){
// do more stuff
}else if(str == "something different"){
// do more stuff
}else if(str == "something really different"){
// do more stuff
}
I am afraid this will become “slow” after i have to repeat those else if lines a lot…
I tried to use switch() statement, but obviously it doesnt work here, is there something similar to switch() to use here?
If you just want to execute different functions, you can use a map from strings to function pointers or functors like
boost::function/tr1::function:As for passing parameters, with the details given so far i’d probably go for passing the unparsed remainder of the line or a list of string tokens to the functions and let them handle those as they see fit:
You could also take a look at the interpreter example from Boost.FunctionTypes (header, source file) wether that is similar to your scenario.