Skip to main content
Module 5 · Lesson 1 of 211/26 in the course~14 min
Module lessons (1/2)

Arrays and Vectors

Lesson preview5 hands-on exercises readyCourse C++ · Module 5 — Data Structures and Pointers

C-style static arrays, std::array and dynamic vectors std::vector. Adding, accessing and iterating.

What you'll learn in this lesson

  • Choose wisely between array C (T arr[N]), std::array<T, N> (secure fixed stack) and std::vector<T> (resizable contiguous heap).
  • Insert and explore data with .push_back(), .size(), range-for, and controlled access .at().
  • Understand the difference between size() and capacity() avoiding repeated reallocations thanks to .reserve().

Overview

Contiguous memory is the secret to performance in C++: when elements live next to each other, the processor can prefetch them in bulk into the L1 cache.

An array C (int arr[5]) has a fixed size on the Stack, but easily decays into a simple pointer losing its length. Since C++11, std::array<T, N> offers zero overhead while maintaining .size() and STL compatibility:

Sections covered in this lesson

  1. 11. C-Style Arrays vs std::array<T, N>
  2. 22. std::vector<T>: The Default Container in C++
  3. 33. Quick Access [] vs Secure Access .at()
  4. 44. size(), capacity() and .reserve(n)
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 5 — Data Structures and Pointers

Unlock "Arrays and Vectors" and the full Course C++ curriculum

C-style static arrays, std::array and dynamic vectors std::vector. Adding, accessing and iterating.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course C++ plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.