Module lessons (1/2)
Arrays and Vectors
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) andstd::vector<T>(resizable contiguous heap). - Insert and explore data with
.push_back(),.size(), range-for, and controlled access.at(). - Understand the difference between
size()andcapacity()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
- 11. C-Style Arrays vs
std::array<T, N> - 22.
std::vector<T>: The Default Container in C++ - 33. Quick Access
[]vs Secure Access.at() - 44.
size(),capacity()and.reserve(n) - 5Practical exercises
- 6Check for understanding
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.