2D vector problem

Nov 3, 2014 at 11:47pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout<<"How many points are there?\n";

	int number;

	cin>>number;

	vector<vector<node*> >points(number,vector<node*>(0));

	for(int i=0;i<number;i++)		//labels each point
	{
		node* newNode=new node;

		points[i][0]=newNode;
		cout<<"Label point "<<i+1<<"\n";

		cin>>points[i][0]->label;
	}

Why isn't this code working? im trying to assign a pointer to the vector elements, but the program terminates once it gets to the "points[i][0]=newNode" Anyone know whats wrong?
Last edited on Nov 3, 2014 at 11:47pm
Nov 4, 2014 at 1:39am
Each inner vector has a size of 0, so [i][0] is out of bounds.
Last edited on Nov 4, 2014 at 1:39am
Nov 4, 2014 at 1:42am
try points[i][0] = *newNode;
Nov 4, 2014 at 1:47am
Well actually you'd need to do points[i].push_back(newNode);
Also that wouldn't work because you're trying to deference it, type mis-match.
Last edited on Nov 4, 2014 at 1:49am
Topic archived. No new replies allowed.