C++20 sections · 1024 units
Open in Course

Adding Elements

Growing your vector

To add an element to the end, use push_back(). Write numbers.push_back(42); and the vector grows by one, with 42 as the new last element. Call push_back as many times as you want.

The vector resizes automatically. Perfect for reading unknown amounts of input or building lists dynamically. Each push_back adds to the end, so order matches the order you added. Push 10, then 20, then 30, the vector contains {10, 20, 30}.