C++ STL Vectors
`std::vector<T>` is a dynamic array from the Standard Template Library that automatically grows and shrinks as you add or remove elements. It's the default go-to container for most C++ programs and competitive programming solutions.
Common operations include `.push_back()` to add an element, `.size()` to get the count, `.pop_back()` to remove the last element, and `[]` or `.at()` for indexed access.
Creating and modifying vectors
`std::vector<int> v;` starts empty; `v.push_back(5);` adds 5 to the end. `v.size()` tells you how many elements are currently stored.
Iterating a vector
Use a range-based for loop `for (int x : v)` or index-based `for (int i = 0; i < v.size(); i++)` to visit every element.
#include <vector>
std::vector<int> v;
v.push_back(1);
v.push_back(2);
std::cout << v.size() << " " << v[0];2 1push_back adds elements; size() and [] inspect the vector.
std::vector<int> v = {5, 10, 15};
for (int x : v) std::cout << x << " ";5 10 15 Range-based for loops over every vector element.
Key points
- std::vector is a dynamic, resizable array (needs #include <vector>).
- push_back() appends; pop_back() removes the last element.
- size() returns the current element count.
- Vectors support [] indexing like arrays.
