Lesson 3 of 45 · C++
Lesson 11: Arrays
Duration: 9 min
Lesson 3 of 45 · C++
Duration: 9 min
std::array (a thin wrapper) for better type safety and value semantics.\n\n---\n\n## Example: C‑style array vs. std::array\ncpp\n// C‑style array\nint numbers[5] = {1, 2, 3, 4, 5};\nfor (int i = 0; i < 5; ++i) {\n std::cout << numbers[i] << ' ';\n}\nstd::cout << '\\n';\n\n// std::array\nstd::array<int, 5> arr = {10, 20, 30, 40, 50};\nfor (auto v : arr) {\n std::cout << v << ' ';\n}\nstd::cout << '\\n';\n\n\nBoth representations store elements contiguously, but std::array participates in the STL ecosystem (e.g., can be passed to algorithms) and supports .size(), .front(), .back().\n\n> Tip: Use std::array whenever the size is known at compile time; reserve raw arrays for interfacing with legacy C APIs.\n\n---\n\n<Alert type="info">Array bounds are not checked by the language. Consider std::vector or std::array's at() for safe access.