0

First off, yes I'm new to C++ vectors.

Now my question is, why do you think my vector is empty?

I have a global variable:

vector<int> parentVector;

Now, since it's a global variable, and I don't know what size should it be, only later into the program, I do this at some point:

//numberOfNodes = 8 in current example
parentVector.reserve(numberOfNodes);

And a little later, I try to assign a value to the first element of the vector, but it doesn't work:

parentVector[0] = -1;

And I also do the following, which doesn't assign any value either:

//nextNode.node and currentNode.node is between the range of 1 - 8, thus the - 1.
parentVector[nextNode.node - 1] = currentNode.node - 1;

Edit: Thank you for your answers, resize() worked.

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76

1 Answers1

3

Reserving space in a vector using std::vector::reserve is not the same as adding elements to it. After reserving the vector is still empty. Use std::vector::resize instead.

piokuc
  • 25,594
  • 11
  • 72
  • 102