Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Start with vector<int> v = {4, 2, 7}. The vector contains three elements, so v.size() is 3 and its valid indices are 0, 1, and 2. After v.reserve(6), those facts do not change. The values remain 4, 2, and 7, the size remains 3, and index 3 is still outside the vector's elements.
vector<int> v = {4, 2, 7};
v.reserve(6);The call to reserve changes the storage guarantee instead. The vector now has capacity of at least 6, which means it can hold at least six elements before needing more storage. The extra positions are available for future insertions, but they do not contain vector elements yet. The capacity may be greater than 6, because reserve guarantees a lower bound rather than an exact value.
Immediately after v.reserve(6), which state is correct?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After reserve, push_back(1) adds one new element at the end. The values become [4, 2, 7, 1], the size becomes 4, and the valid indices become 0-3. The earlier values keep their positions. Because the vector already has capacity of at least 6, this operation is guaranteed to fit within that reserved capacity.
v.push_back(1); // v is [4, 2, 7, 1], size is 4
v[1] = 5; // v is [4, 5, 7, 1], size is still 4
v.pop_back(); // v is [4, 5, 7], size is 3The next statement, v[1] = 5, replaces the element already at index 1. It does not add an element, remove an element, or change the size. The vector becomes [4, 5, 7, 1], and its valid indices remain 0-3. Finally, pop_back removes the last element, which is 1. The vector becomes [4, 5, 7] and its size returns to 3.
| STATEMENT | VALUES | SIZE | VALID INDICES |
|---|---|---|---|
| v.push_back(1) | [4, 2, 7, 1] | 4 | 0-3 |
| v[1] = 5 | [4, 5, 7, 1] | 4 | 0-3 |
| v.pop_back() | [4, 5, 7] | 3 | 0-2 |
At every point, the valid index range starts at 0 and ends at v.size() - 1. After reserve, size is 3, so index 3 is invalid even though storage for it is available. After push_back, size is 4, so index 3 becomes valid. After pop_back, size is 3 again, so index 3 becomes invalid again.
Using v[i] outside the valid range has undefined behavior. For example, reading v[4] from the final vector does not safely read an unused reserved slot. The program may print an unexpected value, appear to work, or fail in a way that is difficult to trace. In contrast, v.at(i) checks the range and reports an out-of-range error when i is invalid.
Replace the invalid expression so it prints the final vector's last element.
cout << v[4];Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
You can answer questions about this sequence by recording values, size, valid indices, and the capacity guarantee after each statement. The values and size are exact. The capacity is not exact: after reserve(6), it is guaranteed to be at least 6, but the vector is allowed to allocate more.
| POINT IN THE SEQUENCE | VALUES | SIZE | VALID INDICES | CAPACITY GUARANTEE |
|---|---|---|---|---|
| After construction | [4, 2, 7] | 3 | 0-2 | Not changed by the construction shown |
| After v.reserve(6) | [4, 2, 7] | 3 | 0-2 | capacity >= 6 |
| After v.push_back(1) | [4, 2, 7, 1] | 4 | 0-3 | capacity >= 6 |
| After v[1] = 5 | [4, 5, 7, 1] | 4 | 0-3 | capacity >= 6 |
| After v.pop_back() | [4, 5, 7] | 3 | 0-2 | capacity >= 6 |
The key distinction is between logical contents and allocated storage. push_back increases the size by adding an element, assignment changes a value without changing the size, and pop_back decreases the size by removing the last element. reserve does none of these: it only ensures that future growth up to the requested amount has storage available.