samedi 8 mai 2021

Best strategy to resize a vector in C++ [duplicate]

I have a structure with the following form:

struct Grid {
  int nPoints_;
  std::vector<std::vector<double> > pointList_;
}

During the excussion of the program I will read from a file the number of points and pass it to the variable nPoints_.

I would like to know the best procedure for defining the size of the vector.

After knowing the number of points I need to define the size of my vector to be equal to the number of points.

What is the most efficient way of doing this?

Should I:

  Grid.pointList_.resize( Grid.nPoints_);

or

  Grid.pointList_.reserve( Grid.nPoints_);

Afterwards, I will be reading values from a file and storing them in the vector.

Best Regards

Aucun commentaire:

Enregistrer un commentaire