how to select variables efficiently?

hej! i have to following problem and would like to know, if anyone can think of a more efficitent way to solve it. the rough idea is, that in the end the user of the program should be able to choose, which variables from a struct he wants to use. here is the thing:

lets assume we have a struct with some variables in it:

1
2
3
4
typedef struct TEST
{
 int a, b, c;
};


now we have a function, which should return one of the variables - selected by the user in form of an incoming string:

1
2
3
4
5
6
7
8
9
10
int testfunction (std::string str)
{
 TEST x;
 if(str=="a")
  return x.a;
 if(str=="b")
  return x.b;
 if(str=="c")
  return x.c;
}


now my question is: is there a more efficient way to select the variables from the struct (without all the if-queries) - as in my real program there are more than 20 variables in the struct, also the given string is not only one single string, but a vector of strings with the variables the user wants to use, and it is checked every time if the matching string for a variable is in the stringvector to copy the variable in another array, which is the output of the function. In the end the program has to run with files of ~100MB.

Thanks for any help! :)
It seems to me that a map would be much better than a struct. You could use a string as the key and you could then retrieve the data very easily.
Another option: http://www.cplusplus.com/forum/general/11460/#msg54095 (scroll to the very bottom).

:-]
Hopefully the actual program is a little more complicated than the example because
it is easier for the user simply to access a, b, or c than call a function with some
parameter that ultimately translates to a, b, or c.
Topic archived. No new replies allowed.