Populating an array of stucts

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)
1
2
3
4
5
6
        struct ClassData {
        	
        	std::string rSource;
        	std::string cName;

        } *ResourceData[N_SOURCES];


Code I am using to populate/test output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        MetadataResourceList::iterator i;
        for (i = resources.begin(); i != resources.end(); i++)
        {                   
            MetadataResource * resource = *i;
            
            string resourceName = resource->GetResourceID();
                
            MetadataClassList classes =	
                metadata->GetAllClasses(resourceName);
            MetadataClassList::iterator i;
            for (i = classes.begin(); i != classes.end(); i++)
            {
                MetadataClass * aClass = *i;
                
                if(aClass->GetStandardName() != ""){
                	ResourceData[i].rSource = resourceName;
                	ResourceData[i].cName = aClass->GetStandardName();
                }
            }
        }
        
        cout << "Resource Name: " << ResourceData[0].rSource << " Class Name: " << ResourceData[0].cName << endl;
N_SOURCES is the length of the array.

I'm confused about this: ResourceData[i]
i is an iterator. How can you use it to index an array?

I don't think this string comparison is correct: (aClass->GetStandardName() != "")
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.
For an array though all you would need is
something like off the top of my head

1
2
3
4
5
6
7
8
9
int *arraySize = new int;
std::cin >> *arraySize;

int *myArray = new int[arraySize];

for(int i = 0; i < arraySize; i++)
{
	myArray[i];
}
That is essentially what I have done by doing this.

1
2
3
4
5
6
        struct ClassData {
        	
        	std::string rSource;
        	std::string cName;

        }*ResourceData[resources.size()];


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.

no match for ‘operator[]’ in ‘ResourceData[i]’
iterators are not integers. something.begin()!=0

a dynamic array of structures
http://www.cplusplus.com/reference/stl/vector/

no match for ‘operator[]’ in ‘ResourceData[i]’
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.
Last edited on
I got it all figured out.

Here is what I did.

1
2
3
4
5
6
7
8
9
10
        struct ClassData {
        	
        	std::string rSource;
        	std::string cName;


        }*ResourceData;
        
        
        ResourceData = new ClassData[resources.size()];


Then I can use something like this
1
2
3
4
5
6
int k = 0;

forloop
                    ResourceData[k].rSource = resourceName;
                    ResourceData[k].cName = aClass->GetClassName();
endfor

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.
You have to store the size somewhere if you dynamically allocate it like above.
sizeof is only giving you the size of the pointer in bytes (32-bits/8=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.
1
2
3
4
5
6
7
8
9
Resource : 0
	Class: 0
	Class: 1
Resource : 1
	Class: 0
	Class: 1
Resource : 2
	Class: 0
	Class: 1
What do you mean? You already figured out the vector<vector<T> >.
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.

cout << vec[0][0][0] << endl;

Topic archived. No new replies allowed.