8.4 — Fixed-size lists (array)
Read one box at a time. No rush.
std::array#
If your list size never changes, use std::array. You must give the size up front.
C++
#include <array> std::array<int, 7> dailySteps { 3000, 4500, 8000, 2000, 6000, 9000, 7000 };std::cout << dailySteps.size(); // 7It works almost like a vector: [ ], .at(), .size(), and range-for loops all work. But it can't grow or shrink.
C-style arrays#
You will see older code use plain arrays:
C++
int numbers[3] { 1, 2, 3 };They work, but they don't know their own size and have fewer safety features.
Good habit
Use std::vector by default. Use std::array when the size is fixed and known in advance. Avoid C-style arrays in new code.
Grids (2D lists)#
A list of lists makes a grid:
C++
std::vector<std::vector<char>> board { { 'X', 'O', 'X' }, { ' ', 'X', 'O' }, { 'O', ' ', 'X' },}; std::cout << board[1][2]; // row 1, column 2: 'O'Tip: press ← / → to move between lessons, / to search.