0

Hello guys and gals :)
I admit I don't know whether there is a way to create a dynamic array of objects. I have a task to read a file with the structure:

body_id x_low x_high y_low y_high z_low z_high

body_id x_low x_high y_low y_high z_low z_high

body_id x_low x_high y_low y_high z_low z_high

where <body_id> is a string and the rest are integers(coordinates).

So depending on the body_id, I want to make an array of objects of the class Body with each object having the name <body_id>, containing the coordinates, BUT I want
to do it on the first reading of the file. I read the string, check if the name is among Body array, if it is I add the new coordinates to the Body object, but if it isn't I want to create a new Body object to the array, how do I do it without making a list?

2
Contributors
3
Replies
25
Views
3 Years
Discussion Span
Last Post by Moschops
0

A std::vector or std::array spring to mind.

vector<Body> theVector;
// make a new Body object here
theVector.push_back(newBodyObject);

Use push_back to keep adding them.

0

Thank you, I already started implementing the solution with a linked list, but since I have no idea how to use the templates you are suggesting, could you please tell me where I can learn more about them, my C++ knowledge lacks templates.
Thank you :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.