Lesson 11 of 45
C++Lesson 11: Arrays
9 min
Course progress24%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 11 of 45
C++9 min
Keep these ideas in mind as you work through the lesson.
Arrays provide the simplest form of contiguous storage. In C++ you can declare a static array with a compile‑time size, or you can use std::array (a thin wrapper) for better type safety and value semantics.
std::array// C‑style array
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; ++i) {
std::cout << numbers[i] << ' ';
}
std::cout << '\
';
// std::array
std::array<int, 5> arr = {10, 20, 30, 40, 50};
for (auto v : arr) {
std::cout << v << ' ';
}
std::cout << '\
';
Both representations store elements contiguously, but std::array participates in the STL ecosystem (e.g., can be passed to algorithms) and supports .size(), .front(), .back().
Tip: Use
std::arraywhenever the size is known at compile time; reserve raw arrays for interfacing with legacy C APIs.
<Alert type="info">Array bounds are not checked by the language. Consider std::vector or std::array's at() for safe access.