I implemented a simple class for n-body simulations in C++. However, the class uses a lot of old C style arrays which I want to replace with data structures that the STL offers.
Here is the relevant part of my code that I want to improve:
struct Particle{
double m; // mass
double x[DIM]; // position
double v[DIM]; // velocity
double F[DIM]; // force
};
class Nbody {
private:
const unsigned int n; // number of particles
const double dt; // step size
const double t_max; // max simulation time
std::vector<Particle> p;
public:
~Nbody(){};
Nbody(unsigned int n_, double dt_, double t_max_);
};
Nbody::Nbody(unsigned int n_, double dt_, double t_max_)
: n{n_}, dt{dt_}, t_max{t_max_} {
p = new std::vector<Particle> [n];
}
I tried to use std::vector<Particle>
. But how do I in this case initialize n
particles correctly? My current approach does not work and the compiler throws many errors. How do I do it correctly?