`class Grid<``ValueType`>

The following code,
for example, creates an identity matrix of size `n`

, in which
the elements are 1.0 along the main diagonal and 0.0 everywhere else:

Grid<double> createIdentityMatrix(int n) { Grid<double> matrix(n, n); for (int i = 0; i < n; i++) { matrix[i][i] = 1.0; } return matrix; }

Constructor | ||

O(1) | Initializes a new empty 0x0 grid. | |

nRows, nCols) |
O(N) | Initializes a new grid of the given size. |

nRows, nCols, value) |
O(N) | Initializes a new grid of the given size, with every cell set to the given value. |

Methods | ||

grid) |
O(N) | Returns `true` if the two grids contain the same elements. |

value) |
O(N) | Sets every grid element to the given value. |

row, col) |
O(1) | Returns the element at the specified `row` /`col` position in this grid. |

O(1) | Returns the grid's height, that is, the number of rows in the grid. | |

row, col) |
O(1) | Returns `true` if the specified row and column position is inside the bounds of the grid. |

O(1) | Returns true if the grid has 0 rows and/or 0 columns. | |

fn) |
O(N) | Calls the specified function on each element of the grid. |

O(1) | Returns the number of columns in the grid. | |

O(1) | Returns the number of rows in the grid. | |

nRows, nCols) |
O(N) | Reinitializes the grid to have the specified number of rows and columns. |

row, col, value) |
O(1) | Replaces the element at the specified `row` /`col` location in this grid with a new value. |

O(1) | Returns the total number of elements in the grid. | |

O(N) | Converts the grid to a printable single-line string representation. | |

O(N) | Converts the grid to a printable 2-D string representation. | |

O(1) | Returns the grid's width, that is, the number of columns in the grid. | |

Operator | ||

grid[row][col] |
O(1) | Overloads `[]` to select elements from this grid. |

grid1 == grid1 |
O(N) | Returns `true` if `grid1` and `grid2` contain the same elements. |

grid1 != grid2 |
O(N) | Returns `true` if `grid1` and `grid2` are different. |

ostream << grid |
O(N) | Outputs the contents of the grid to the given output stream. |

istream >> grid |
O(N) | Reads the contents of the given input stream into the grid. |

Grid(); Grid(int nRows, int nCols); Grid(int nRows, int nCols, ValueType value);

Initializes a new grid.
The first form of the constructor creates an empty grid that
contains zero rows and columns.

The second form of the constructor is
more common and creates a grid with the specified number of rows
and columns. Each element of the grid is initialized to the
default value for the type. The default constructor creates an
empty grid for which the client must call `resize`

to
set the dimensions.

The third form also fills every cell of the grid with the given value.

The second and third constructors signal an error if a negative number of rows or columns is passed.

Usage:

Grid<ValueType> grid; Grid<ValueType> grid(nRows, nCols); Grid<ValueType> grid(nRows, nCols, value);

bool equals(const Grid& grid) const;

Returns

`true`

if the two grids are the same size and contain exactly the same element values.
Identical in behavior to the `==`

operator.
Usage:

if (grid.equals(grid2)) ...

void fill(ValueType value) const;

Sets every grid element to the given value.
The entire contents of the grid are replaced with this value in every cell.

Usage:

grid.fill(value);

Available since: 2014/03/01 version of C++ library

ValueType get(int row, int col); const ValueType & get(int row, int col) const;

Returns the element at the specified

`row`

/`col`

position in this grid. This method signals an error if the
`row`

and `col`

arguments are outside
the grid boundaries.
Usage:

ValueType value = grid.get(row, col);

int height() const;

Returns the grid's height, that is, the number of rows in the grid.
This is equivalent to

`numRows`

and both are provided for convenience.
Usage:

int nRows = grid.height();

Available since: 2014/08/16 version of C++ library

bool inBounds(int row, int col) const;

Returns

`true`

if the specified row and column position
is inside the bounds of the grid.
Usage:

if (grid.inBounds(row, col)) ...

bool isEmpty() const;

Returns

`true`

if the grid does not contain any rows or columns (size 0x0).
Usage:

if (grid.isEmpty()) ...

void mapAll(void (*fn)(ValueType value)) const; void mapAll(void (*fn)(const ValueType & value)) const; void mapAll(FunctorType fn) const;

Calls the specified function on each element of the grid. The
elements are processed in *row-major order,* in which
all the elements of row 0 are processed, followed by the elements
in row 1, and so on.

Usage:

grid.mapAll(fn);

int numCols() const;

Returns the number of columns in the grid.

Usage:

int nCols = grid.numCols();

int numRows() const;

Returns the number of rows in the grid.

Usage:

int nRows = grid.numRows();

void resize(int nRows, int nCols);

Reinitializes the grid to have the specified number of rows
and columns. Each element of the newly resized grid is
initialized to the default value for the type.
Any previous grid contents are discarded.

This function signals an error if a negative number of rows or columns is passed.

Usage:

grid.resize(nRows, nCols);

void set(int row, int col, ValueType value);

Replaces the element at the specified

`row`

/`col`

location in this grid with a new value. This method signals an error
if the `row`

and `col`

arguments are outside
the grid boundaries.
Usage:

grid.set(row, col, value);

int size() const;

Returns the total number of elements in the grid,
which is equal to the number of rows times the number of columns.

Usage:

int sz = grid.size();

string toString() const;

Converts the grid to a printable string representation, such as

`"{{r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2}}"`

for a 2x3 grid.
Usage:

string str = grid.toString();

string toString2D() const;

Converts the grid to a printable 2-D string representation, such as the following for a 4x3 grid:

"{{r0c0, r0c1, r0c2},\n {r1c0, r1c1, r1c2},\n {r2c0, r2c1, r2c2},\n {r3c0, r3c1, r3c2}}"

Usage:

string str = grid.toString2D();

int width() const;

Returns the grid's width, that is, the number of columns in the grid.
This is equivalent to

`numCols`

and both are provided for convenience.
Usage:

int nCols = grid.numCols();

Available since: 2014/08/16 version of C++ library

GridRow operator[](int row); const GridRow operator[](int row) const;

Overloads

`[]`

to select elements from this grid.
This extension enables the use of traditional array notation to
get or set individual elements. This method signals an error if
the `row`

and `col`

arguments are outside
the grid boundaries.
Usage:

grid[row][col]