Coding/Learn/cpp/vectors stl
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnDynamic Arrays: std::vector and STL
16 / 20 0%
Lesson 16 of 20
5 mins read

Dynamic Arrays: std::vector and STL

PreviousNext Lesson
In Plain English

Fixed arrays are restrictive because you must know their exact size in advance. The Standard Template Library (STL) provides std::vector—a dynamic array that automatically grows and shrinks as you add or remove elements.

The Mental Model

"If a fixed array is an unexpandable wooden egg carton with 6 slots, a std::vector is an accordion suitcase that automatically expands whenever you pack more items into it."

Deep Dive: How It Works

#include <vector>: Required header.

Declaration: std::vector<int> scores; (an empty vector of integers) or std::vector<std::string> names = {"Alex", "Ada"};.

.push_back(val): Appends an element to the end of the vector and expands memory automatically.

.size(): Returns the current number of elements.

.pop_back(): Removes the last element.

Range-based for loop: for (int x : vec) { std::cout << x; } gives clean, index-free iteration.

Syntax Blueprint

#include <vector>

std::vector<int> v;
v.push_back(10); // Adds 10
v.push_back(20); // Adds 20
for (int x : v) {
  std::cout << x << " ";
}

Dynamic resizable vector with push_back and range-based for loop.

PointCore Rules to Remember

Point star
Automatic Resizing: Grows dynamically as elements are added with push_back().
Point star
The Modern C++ Standard: Preferred over raw C-style arrays in 95% of modern C++ code.
Point star
Range-Based Loops: Use for (const auto &item : vec) for clean, high-performance iteration.

Common Beginner Traps & How to Fix Them

Mistake:Using [] on an empty vector: vec[0] = 5;

Why it happens: [] modifies an existing index, but cannot add a new slot to an empty vector.

How to fix: Use vec.push_back(5) to add new elements.

Pro Tip: If you know how many elements you'll insert, calling vec.reserve(1000) prevents intermediate reallocations!

Live Interactive Example

Hit Run Code to see it live
Managing a Dynamic Inventory with std::vector
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: std::vector handles dynamic memory allocation and resizing behind the scenes.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Build a numbers vector

  • PointDeclare std::vector<int> nums;
  • PointPush 100 and 200 using push_back().
  • PointPrint "Vector size: 2".
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates