CurriculumVector
Vector
A <code>std::vector</code> is a dynamic array that can grow in size automatically. It guarantees that its elements are stored contiguously in memory, meaning you can access them instantly using an index like <code>vec[3]</code>.
011. Initialization & Basics
A vector can be initialized in multiple ways. The easiest is using an initializer list.
main.cpp
Terminal
Waiting for execution...
022. Size vs Capacity
Size is how many elements are currently in the vector. Capacity is how much space is allocated before it needs to grow. When size reaches capacity, the vector automatically reallocates a larger block of memory.
main.cpp
Terminal
Waiting for execution...
033. Element Access
You can access elements using `[]` (fast, no bounds checking) or `.at()` (slower, throws an exception if out of bounds).
main.cpp
Terminal
Waiting for execution...
044. Modifiers (Push & Pop)
`push_back` adds an element to the end. `pop_back` removes the last element. Both are extremely fast O(1) operations.
main.cpp
Terminal
Waiting for execution...
055. Iterators
Iterators act like pointers pointing to elements inside the container. `begin()` points to the first element, and `end()` points to one past the last element.
main.cpp
Terminal
Waiting for execution...
066. Insert & Erase
Inserting or erasing elements in the middle of a vector requires shifting all subsequent elements, making it an O(N) operation.
main.cpp
Terminal
Waiting for execution...
077. Clear & Empty
`clear()` removes all elements (size becomes 0), but it does NOT free the memory capacity. `empty()` checks if size is 0.
main.cpp
Terminal
Waiting for execution...
088. Reserve vs Resize
`reserve(N)` allocates memory for N elements without creating them. `resize(N)` actually creates N elements (filling with 0 if necessary).
main.cpp
Terminal
Waiting for execution...
099. 2D Vectors
A vector of vectors can be used to create a 2D grid or matrix.
main.cpp
Terminal
Waiting for execution...
1010. Math & Vector Reductions
Let's triple check our math logic by computing a sum of elements in a vector.
main.cpp
Terminal
Waiting for execution...
AI Tutor
Vector is the most heavily used container in C++. Because it stores data contiguously, it is incredibly cache-friendly and fast. You should use vector by default unless you have a very specific reason to use something else.