C++ Tutorial: Arrays & Vectors
C arrays, std::vector, std::array, indexing, traversal, and when to use each.
Collections of data
C++ has three main array types:
- **C array** (`int arr[5]`) — fixed size, legacy, no bounds checking
- **`std::array<T, N>`** — fixed size with bounds checking and STL compatibility
- **`std::vector<T>`** — dynamic size, the default choice for almost everything
Always prefer `std::vector` unless you have a specific reason for fixed size.
What you'll learn in this C++ arrays & vectors tutorial
This interactive C++ tutorial has 7 hands-on exercises. Estimated time: 18 minutes.
- std::vector — the dynamic array — `vector` grows automatically. It's the C++ equivalent of Python's list or Java's ArrayList.
- Iterate a vector — Three ways to iterate a vector:
- Vector methods — Key vector operations:
- C arrays and std::array — C arrays are the original — fixed size, no bounds checking, legacy.
- Find min, max, sum in a vector — Calculate statistics by iterating. The `<algorithm>` header has `min_element`, `max_element`, and `accumulate` from `<nu…
- 2D vector — matrix — A `vector<vector<T>>` creates a 2D structure — like a matrix or grid:
- Build a grade analyser — No starter code.
C++ Arrays & Vectors concepts covered
- Collections of data