not understanding "push_back" if used recursively

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;

typedef struct Record
{
    std::string    name;
    bool	   isVisible;
    int	           index;
}Record;
vector<Record> recordVector;

typedef struct Group
{
	vector<Record> record;
	int item;
} Group;

vector<Group> groupVector;

int main (int argc, char * const argv[])
{
	Record tmpRecord = {"c++", true, 1};
	
	for (int i = 0 ; i < 15; ++i) {
		recordVector.push_back(tmpRecord);
	}
	
	for (int j = 0; j < 5; ++j) {
		Group tmpGroup;
		tmpGroup.record.push_back(tmpRecord);
		tmpGroup.item = j;
		groupVector.push_back(tmpGroup);
	}
	
    return 0;
}


Any one please let me know where i'm getting wrong any why. I think we can't push vector like this, can any explain me the reason. Things are not getting clear to me.

Thanks.
There is no recursion here - just loops.
What is it that you dont understand exactly?
Yeah is running smoothly but when i am debugging it(in XCode IDE) nad hover over the groupVector variable after the loop gets executed, then under groupVector the record contents is not showing.
Well, I can't do to much from my iPod (very hard to post), but you are creating the record vector with 15 elements in the first loop and then not using it in the second loop. RecordVector seems to be unnecessary. I wouldn't recommend using it any way since pushing it back will copy it. The result of your second loop is to create a vector of 5 groups but with only 1 record in each group, not 15.

1
2
3
4
5
6
7
8
9
10
11
vector<Group> groups(5);  // make size 5 Groups from the start
Record tmpRecord = { "c++", true, 1 }; // you should have a constructor that takes these arguments

// initialize the groups
for (int i=0; i<5; ++i) {
  groups[i].item = i;
  groups[i].record.resize(15); // make size 15 
  for (int j=0; j<15; ++j) {
    groups[i].record[j] = tmpRecord;
  }
}


Topic archived. No new replies allowed.