I am working on an application where I need to store data in an array of stucts. The issue that I am running into is that I do not know the length of the array, I will never know the max length of the array. So I am kind of stuck at an point to where I am not sure exactly what to do.
Here is the code that I am currently working with. Any help with this is greatly appreciated.
Struct: (N_SOURCES is defined, but with out a length)
N_SOURCES was from a previous bit of testing that I was doing. Due to the fact that I will never know the max length of the array, I need to find a way to create a dynamic array of structures.
The reason I used i was to create the index for the array. IE on the first iteration i = 0 and the first index in the array would be 0.
As for the sting comparison it is just temporary until I get the structure populated and for ease of display during testing.
resources.size() gives me the total number of resources I will have. Now, the issue is this. When I try to populate the array of structures with the code below. I always get this error.
I'm not sure about that error. ResourceData is a 'ClassData **', to it should interpret the [] as the common offset operator, not as an overloaded operator.
I do have one other question. What would be the best way to get the length of the array of structures. I have tried using sizeof(), but for some reason no matter how many indexes there are I always end up with the value of 4.
After spending some time working on this, I have come to the conclusion that this may not be the preferred way to handle this. After a bit of looking into other ways I could do this. I have realized that a multidimensional array would better suit what I am trying to do.
With a bit of testing I have come up with this simple vector array.
1 2 3 4 5 6 7 8 9 10 11 12 13
vector< vector<int> > vec;
for (int i = 0; i < 3; i++) {
vector<int> row;
row.push_back(i);
vec.push_back(row);
}
for(int k = 0; k < vec.size(); k++){
for(int l = 0; l < vec[k].size(); l++){
cout << "Resource : " << vec[k][l] << endl;
}
}
This gives me the output of.
1 2 3
Resource : 0
Resource : 1
Resource : 2
The one thing I can not figure out is how to create a subvector of "Resource" so I would wind up with an output something like this.
Well what I am trying to do is create a multidimensional vector array where each parent array element can have multiple children array elements. My last code block in my previous post was some what of an example.
Here is a better one.
If I have the parent array element 0 how would I write it so that 0 could have multiple children array elements. Right now if I want to access the data in the current implementation that I have I could do something like below. But this however would only give me the parent element.
cout << vec[0][0] << endl;
If I wanted to access the child array element I would access it something similar to this.