C++ vectors and iterator invalidation

A vector stores elements contiguously and grows as needed. push_back appends; at performs bounds checking and throws out_of_range for an invalid index.

C++
#include <iostream>
#include <vector>
#include <cassert>
int main() {
    std::vector<int> values{2, 4};
    values.push_back(6);
    values.at(1) = 5;
    for (int value : values) std::cout << value << '\n';
    assert(values.size() == 3);
}
Read the full lesson