Reputation: 37349
I have a space separated file which contains some key->value pairs. These are to be loaded to a structure of the type given below:
#define FV_PARAM1 "A"
#define FV_PARAM2 "B"
parameter_t & parameterFeatureVector (
parameter_t & param,
int param1,
int param2,
) {
param.addParam(FV_PARAM1, param1);
param.addParam(FV_PARAM2, param2);
return param;
}
So to the above, I can pass the following values:
parameterFeatureVector( 10, 20 );
And I would expect the same to get loaded to 'param' structure. The above values are taken from the file. How would I go about implementing the same. If the above is not clear, do feel free to get back on it.
Upvotes: 0
Views: 214
Reputation:
I take it you are asking how to translate a name "A" to a specific structure field? If So, C++ has no built-in way of doing that - you have to write a function:
void Add( parameter_t & p, const std::string & name, int value ) {
if ( name == "A" ) {
p.param1 = value;
}
else if ( name == "B" ) {
p.param2 = value;
}
else if ( .... ) { // more name tests here
}
}
However, I would suggest not doing that, and instead use a map:
std::map <std::string, int> params;
you can then say things like:
params["A"] = 42;
Upvotes: 1
Reputation: 35510
I assume you are trying to manage a collection of key value pairs. I am not sure the type of the key value pairs used in the file.
You can use associative container std::map for this purpose.
std::map<Ta,Tb> aCollectionOfKeyValuePairs;
where
For ex: if both Ta and Tb are int
then,
std::map<int,int> aCollectionOfKeyValuePairs;
void parameterFeatureVector ( int param1, int param2)
{
//insert into the map key==>value
aCollectionOfKeyValuePairs[param1] = param2;
}
Then inorder to insert you can call:
parameterFeatureVector( 10, 20 );
Upvotes: 0