Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 171 ยท Arrays and Vectors

Multidimensional Arrays and Matrices

Store grids and matrices in C++ with 2D arrays, std::array of arrays, vector of vectors and fast flat vectors with index math, plus row-major order and std::mdspan.

Plenty of data is naturally a grid: a chessboard, the pixels of an image, a spreadsheet, a seating plan, a matrix in a physics simulation. C++ has no single built-in "matrix" type. Instead you build grids out of the containers you already know, and the choice you make affects safety, flexibility and, quite dramatically, speed.

This lesson walks through the four common ways to store a two-dimensional grid: nested C arrays, std::array of std::array, std::vector of std::vector, and one flat std::vector with index arithmetic. Along the way you'll learn how grids are laid out in memory (row-major order) and why the order of your loops matters. It ends with C++23's std::mdspan, the standard way to view flat data as a multidimensional array.

Two-dimensional C arrays

A 2D C-style array is an array of arrays. int grid[3][4] is an array of 3 rows, and each row is an array of 4 ints. You index it with two subscripts, row first: grid[row][col].

C++
int grid[3][4] = {
    {1, 2, 3, 4},      // row 0
    {5, 6, 7, 8},      // row 1
    {9, 10, 11, 12},   // row 2
};
int seats[2][5]{};     // all zero

The initialization rules from C-Style Arrays apply at every level: missing rows or elements become zero. Only the first dimension can be left out and deduced: int grid[][4] = {...} is fine, int grid[][] = {...} is not.

Row-major layout

In memory, the 12 elements are stored in one contiguous block, row after row. This is called row-major order, and C, C++ and most languages derived from them use it:

TEXT
grid[0][0] grid[0][1] grid[0][2] grid[0][3] grid[1][0] grid[1][1] ... grid[2][3]
    1          2          3          4          5          6      ...     12

So grid[r][c] lives at position r * 4 + c from the start. Keep that formula in mind; it comes back later. (Fortran, MATLAB and many math libraries use column-major order instead, which matters when you exchange data with them.)

C++ A 2D C array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Two details deserve attention. First, print_grid must spell out the column count: grid decays to a pointer to its first row, const int (*)[4], and the compiler needs the row length to compute grid[r][c]. A function for 4-column grids can't take a 5-column grid. Second, the outer range-based loop uses const auto& row; with plain auto, row would decay to a pointer and the inner loop wouldn't compile.

Higher dimensions work the same way: float voxels[16][16][16] is an array of 16 arrays of 16 arrays of 16 floats. Raw 2D arrays inherit every weakness of raw 1D arrays (no bounds checks, decay, no copying), so in new code use the options below.

std::array of std::array

For a grid whose size is fixed at compile time, nest std::array. The outer array holds rows, the inner type is one row:

C++
std::array<std::array<char, 3>, 3> board{};   // 3 rows of 3 chars

It has the same memory layout as char board[3][3], but it is a value: you can copy it with =, compare it with ==, return it from functions and pass it by reference without losing its size. A type alias keeps declarations readable:

C++ A tic-tac-toe board

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

X wins through the right column. board[r][c] first picks row r (a std::array<char, 3>), then element c of that row. Use board.at(r).at(c) when indexes come from user input. The Tic-Tac-Toe project builds a full game on this representation.

std::vector of std::vector

When the dimensions are only known at runtime, the simplest tool is a vector whose elements are vectors:

C++
std::size_t rows = 3, cols = 4;
std::vector<std::vector<double>> m(rows, std::vector<double>(cols, 0.0));

Read the constructor as "rows copies of a row that holds cols zeros". You then index with m[r][c] and get the size with m.size() (rows) and m[r].size() (columns in row r). Rows can have different lengths, which is perfect for jagged data such as the Pascal's triangle in std::vector or a list of words per line.

C++ Matrix multiplication with nested vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The Input box holds a 2ร—3 matrix and a 3ร—2 matrix, each preceded by its dimensions, and the product is [58, 64] and [139, 154]. Change the numbers or the sizes and run again. Notice the loop order i, k, j in multiply: the innermost loop walks along a row of b and a row of result, which is the memory-friendly direction. More on that below.

Nested vectors are convenient, but they have real costs:

  • Each row is a separate heap allocation. A 1000ร—1000 matrix makes 1001 allocations, and the rows can be scattered around memory.
  • Nothing keeps it rectangular. One m[2].push_back(x) and row 2 is longer than the others; every function has to trust or check that the rows agree.
  • Two indirections per access. m[r][c] first loads the row vector's pointer, then the element.

For small or jagged data, none of that matters. For numeric work, image processing and anything performance-sensitive, use a flat layout.

Flat storage: one vector and index math

Remember the row-major formula? You can apply it yourself: store all rows * cols elements in one std::vector, and compute the position of (r, c) as r * cols + c. It's the layout a C array has, with runtime sizes, a single allocation and guaranteed rectangular shape. Practically every serious matrix and image library works this way.

Wrapping the index math in a small type keeps the formula in one place. This uses a struct with member functions; Classes and Objects explains them in detail, and the Matrix Library project builds a complete version.

C++ A flat grid with index math

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Since C++23, operator[] may take more than one argument, so a custom type can be indexed as heat[r, c], which reads like math. In older code you'll see operator()(r, c) used the same way, written heat(r, c). (A comma inside [] meant the comma operator before C++20 deprecated it, which is why the new syntax had to wait.) The whole grid is 24 ints in a single vector; copying it, clearing it or handing its data() pointer to a C library is trivial.

Loop order and the cache

Processors don't fetch single numbers from memory. They fetch cache lines, typically 64 bytes, and they prefetch the next ones when you walk through memory in order. With row-major data, the inner loop should therefore run over columns (c), moving to the neighbor in memory each time. Swapping the loops makes every step jump a whole row ahead, which defeats the cache:

C++ Row order vs column order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Both orders add the same four million numbers, so both sums are 4000000. On typical hardware the column-order runs take several times longer than the row-order runs; the exact numbers depend on the machine and on what else the server is doing, so run it a few times. The data structure didn't change, only the access pattern. Writing Fast C++ returns to cache-friendly design.

std::mdspan (C++23)

The flat-grid pattern is so common that C++23 standardized it as std::mdspan in the <mdspan> header: a non-owning view that presents a pointer to contiguous data as a multidimensional array, with the index math done for you. Like std::span, it doesn't own or copy anything; you keep the data in a std::vector (or any buffer) and look at it through the view.

C++
#include <mdspan>
#include <print>
#include <vector>

int main() {
    std::vector<double> storage(3 * 4, 0.0);
    std::mdspan grid(storage.data(), 3, 4);   // 3 rows, 4 columns, row-major

    for (std::size_t r = 0; r < grid.extent(0); ++r) {
        for (std::size_t c = 0; c < grid.extent(1); ++c) {
            grid[r, c] = r * 10.0 + c;
        }
    }
    std::println("{}", grid[2, 3]);           // 23
    std::println("{}", storage[2 * 4 + 3]);   // 23: same element
}

extent(0) and extent(1) give the row and column counts. Extents can also be fixed at compile time (std::extents<std::size_t, 3, 4>), and layouts let the same view describe column-major or strided data (std::layout_left, std::layout_stride), which is how C++ code shares buffers with Fortran-style libraries.

The code above is read-only because the standard library on our server (GCC 15's libstdc++) doesn't ship <mdspan> yet. Clang's libc++ and Microsoft's library already provide it, and the open-source reference implementation (kokkos/mdspan) works with older compilers. Until it's everywhere, a small flat-grid type like the one above does the same job.

Choosing a representation

Representation Size known Memory Good for
T grid[R][C] Compile time One block Legacy code and C interop
std::array<std::array<T, C>, R> Compile time One block, a value type Small fixed boards, lookup tables
std::vector<std::vector<T>> Runtime One allocation per row Jagged data, quick prototypes
Flat std::vector<T> + r * cols + c Runtime One block Matrices, images, simulations
std::mdspan over a buffer Either Views existing memory Sharing grids between functions and libraries

Avoid huge std::array grids as local variables (they live on the stack), and avoid nested vectors in hot numeric loops.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In int grid[3][5];, how many elements does each row have, and how many rows are there?

    int grid[3][5] is an array of 3 rows, and each row is an array of 5 ints. You index it as grid[row][col].
  2. 2. A flat std::vector<int> stores a grid with cols columns in row-major order. Where is the element at row r, column c?

    Row-major order stores whole rows one after another, so you skip r full rows (r * cols elements) and then move c elements into the row.
  3. 3. Why must void f(int g[][4], std::size_t rows) include the 4?

    g decays to a pointer to rows of 4 ints. To find g[r][c], the compiler skips r rows of 4 elements each, so the column count must be part of the type.
  4. 4. What does std::vector<std::vector<int>> m(2, std::vector<int>(3, 7)); create?

    The outer vector gets 2 copies of the inner vector, and each inner vector holds 3 elements with the value 7.
  5. 5. For a large row-major grid, which loop order is usually much faster for summing all elements?

    With rows outside and columns inside, each step reads the next element in memory, so the CPU cache and prefetcher work at full speed.

Summary

  • A 2D C array T g[R][C] is an array of R rows of C elements, stored contiguously in row-major order; element [r][c] sits at offset r * C + c.
  • Passing a 2D C array to a function requires every dimension except the first in the parameter type.
  • std::array<std::array<T, C>, R> gives a fixed-size grid with value semantics; type aliases keep it readable.
  • std::vector<std::vector<T>> handles runtime and jagged sizes but costs one allocation per row and doesn't enforce a rectangle.
  • A flat std::vector<T> with r * cols + c indexing is the fast, standard layout for matrices and images; C++23's grid[r, c] subscript makes it pleasant to use.
  • Loop with rows outside and columns inside to stay cache-friendly.
  • C++23's std::mdspan views flat data as a multidimensional array (not yet available in GCC 15's library).

Next: std::span, a lightweight view that lets one function work with C arrays, std::array and std::vector alike.