samedi 25 septembre 2021

C++ allocate variables depending on Type of data in vector

I would like to create a function that is able to retrieve some information from files with scalar and or vector values.

The files have the following form:

If they are scalars:
some file header and information
data values
1
2
3
4

if they are vectors:
some file header and information
data values
(1 2 3)
(4 5 6)
(7 8 9)

I have written the function to collect the information from the file. Now I would like to create a function where the user passes a template type and it will look into the file and store the information in a vector<Type>

Currently, I have:

typedef std::vector<double> scalarField;
typedef std::vector<std::array<double, 3>> vectorField;

int main(int argCount, char *args[])
{

    vectorField a = readFile<vectorField> (filename);
    return 0;

}

In the readFile function I have:

template <typename Type>
Type readFile(std::string fileName)
{
     // create a vector to check data type and push_back data accordingly
    Type store;

    // routine to read the file and get the lines of interest

    // If it sees a vector of doubles
    if (*typeid(store[0]).name() == 'd')
    {
       // define a istringstream named iss  and get the double value
       iss >> variableToGetDouble;
       store.push_back(variableToGetDouble);
   }
   else if (*typeid(store[0]).name() == 'S')
   {
       // define a istringstream named iss  and x, y ,z doubles 
       store.push_back(std::array<double,3>{x,y,y})
   }
}

However, I get an error no known conversion for argument 1 from ‘double’ to ‘std::vector<std::array<double, 3> >::value_type&& {aka std::array<double, 3>&&}

I would like to know if it is possible to allocate variables depending on the data type in the vector. Am going in the right track or is there a better solution?

Best Regards

Aucun commentaire:

Enregistrer un commentaire