stack with variable size

Good day people,

I would like to have a couple of arrays of doubles with variable sizes, it could be 20, it could be 500 000.
I need access to each item individually with an index.
The size may vary during the execution of the program.
I've no idea how to do this, and I'd like to have some ideas.

Thanks for reading and hopefully you can help me out,
Rob Bresseleers
aka dirtcrusher
Use new[] and delete[].
Last edited on
You can use a vector
eg:
1
2
3
vector<double> V ( 20 ); // creates a vector of 20 doubles
V.resize ( 500000 ); // changes the size to 500000, BTW why so many elements?
V[0] = 5; // access elements as with arrays 

http://www.cplusplus.com/reference/stl/vector/
The C++ way is to use a vector.

1
2
vector<double> aNiceShinyVector (x);
aNiceShinyVector[3] = 0.4; // example setting value in the third element of your vector 


where x is some value you'd like to set the size to at the start, for convenience, but this can be determined at runtime and you can change the size when you like.

http://www.cplusplus.com/reference/stl/vector/
Last edited on
Ok thanks.
I need so many elements because the vector will be containing the position and velocity of particles.

Thanks a lot for the fast replies,
Rob Bresseleers
aka dirtcrusher
Topic archived. No new replies allowed.