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. | |
O(N) | Initializes a new grid of the given size. | |
O(N) | Initializes a new grid of the given size, with every cell set to the given value. | |
Methods | ||
O(N) | Returns true if the two grids contain the same elements. |
|
O(N) | Sets every grid element to the given value. | |
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. | |
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. | |
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. | |
O(N) | Reinitializes the grid to have the specified number of rows and columns. | |
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 | ||
O(1) | Overloads [] to select elements from this grid. |
|
O(N) | Returns true if grid1 and grid2 contain the same elements. |
|
O(N) | Returns true if grid1 and grid2 are different. |
|
O(N) | Outputs the contents of the grid to the given output stream. | |
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);
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;
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;
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;
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;
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;
true
if the specified row and column position
is inside the bounds of the grid.
Usage:
if (grid.inBounds(row, col)) ...
bool isEmpty() const;
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;
Usage:
grid.mapAll(fn);
int numCols() const;
Usage:
int nCols = grid.numCols();
int numRows() const;
Usage:
int nRows = grid.numRows();
void resize(int nRows, int nCols);
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);
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;
Usage:
int sz = grid.size();
string toString() const;
"{{r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2}}"
for a 2x3 grid.
Usage:
string str = grid.toString();
string toString2D() const;
"{{r0c0, r0c1, r0c2},\n {r1c0, r1c1, r1c2},\n {r2c0, r2c1, r2c2},\n {r3c0, r3c1, r3c2}}"
Usage:
string str = grid.toString2D();
int width() const;
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;
[]
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]