views:

129

answers:

2

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.

+1  A: 

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 usie a map:

std::map <std::string, int> params;

you can then say things like:

params["A"] = 42;
anon
A: 

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

  • Ta - type of the parameter A
  • Tb - type of the parameter B

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 );
aJ