std::vector Basics: push_back size capacity
Create, grow and inspect vectors and understand the capacity vs size distinction.
Why std::vector?
std::vector is the default container in C++. It grows automatically, manages memory for you, and gives O(1) random access. Use it unless you have a specific reason not to.
Including and Declaring
Include <vector> and declare with the element type as a template parameter.
#include <vector>
std::vector<int> nums;
std::vector<std::string> names = {"Ada", "Bob"};All lessons in this course
- std::vector Basics: push_back size capacity
- Iterating Vectors: index range-for iterators
- Modifying Vectors: insert erase clear
- Vector vs std::array vs C Array