How to store this data structure in memory?

Hello,

I'm engineer switching from ancient Fortran to C++. My OOP skills are dim at best and would appreciate your help on storing the data structure I'm going to explain below.

I need to read a file that includes some engineering results data (namely ANSYS FE stress data for those familiar). To extract only the data I need, I have to read an input file first. Anyway, the input file includes the following data structure (just a simple analogy);

Name of triangle; --> Integer --> 1
Type of triangle; --> String --> 1
Min height and max height --> Float --> 2
Possible Colors --> String --> unknown number

The input file has an unknown number of triangles and each triangle has different unknown possible colors. In addition, each one has a type and min/max height.

I thought about creating an triangle object type, but the number of triangles are known at run-time. I don't know how to create a finite number of triangle objects with different names at runtime (as far as I know it's only possible in run-time compiled languages, Java).

Please, let me know how to read and store this type of data in the most efficient way. My only option at hand is now below and doesn't seem an efficient way to do it;

Read number of triangles first;
Loop for all triangles and read the data, store number of possible colors in a vector variable;

Open the engineering results data file;
Loop for every triangle and search for possible colors;

Thanks in advance,

Arda



If you don't know how many of them there are going to be, just store them in a vector:

vector<triangle> triangleStore;

Create each triangle as needed, and add it to the vector:

triangleStore.push_back(someTriangleObject);

There is no need to know in advance how many there will be.
Last edited on
Thanks for the reply. However, I miss one point. How do I declare a triangle struct each time? I mean what will "someTriangleObject" be?

Define the triangle type.
1
2
3
4
5
6
struct triangle
{
  int name;
  string type;
  // etc etc
};



Create one, load it up with data, put it in the vector, repeat until all data is stored:

1
2
3
4
5
6
7
8
9
10
triangle someTriangleObject;
vector<triangle> triangleStore;

while ( thereIsStillDataLeftToRead)
{
  getInputData(someTriangleObject);
  triangleStore.push_back(someTriangleObject);
}

// Now have a vector of triangles, containing all the triangle data read in 




Last edited on
Thanks, Moschops.
Topic archived. No new replies allowed.