Quick question. Let's say I declare a vector of size 20. And then I want to add a few integers to it using push_back.
vector<int> myVector(20);
myVector.push_back(5);
myVector.push_back(14);
Is the capacity of my vector now 22, or is it still 20? Were 5 and 14 added to indices [19] and [20], respectively? Or are they at [0] and [1]?
After those statements its capacity is implementation-defined. (Please note that is different from its size.)
vector<int> myVector(20);
This creates a vector filled with twenty 0's. Its size is twenty, exactly, and its capacity is at least twenty. Whether or not it's exactly twenty is implementation-defined; it may have more (probably not, in practice).
myVector.push_back(5);
After this, the twenty-first element of the array is 5, and the capacity is once again implementation-defined. (If the capacity had been exactly twenty before, it is now increased in an unspecified manner.)
myVector.push_back(14);
Likewise, now the twenty-second element of the array is 14, and the capacity is implementation-defined.
If you want to reserve space, but not insert elements, you'd do it like this:
vector<int> myVector;
myVector.reserve(20); // capacity is at least twenty, guaranteed not
// to reallocate until after twenty elements are pushed
myVector.push_back(5); // at index zero, capacity at least twenty.
myVector.push_back(14); // at index one, capacity at least twenty.