Initialize a matrix in C++ with specific value
This post will discuss how to initialize a matrix in the C++ programming language.
A matrix, also known as a two-dimensional array, is basically an array of arrays. This post provides an overview of some of the available alternatives to initialize a matrix in C++:
1. Using Initializer list
We can initialize a fixed-length matrix with a value of 0 if we provide an empty initializer list or specify 0 inside the initializer list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <algorithm> using namespace std; int main() { const size_t M = 5; const size_t N = 5; // empty initializer list int mat[M][N] {}; // print the matrix return 0; } |
2. Using std::fill function
To initialize a fixed-length or variable-length matrix by any value in C++, the recommended solution is to apply the standard algorithm std::fill from the algorithm header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <algorithm> using namespace std; int main() { size_t M = 5; size_t N = 5; const int value = 1; int mat[M][N]; std::fill(*mat, *mat + M*N, value); // print the matrix return 0; } |
This works since two-dimensional arrays are interpreted as a one-dimensional array in C++, and they can be entirely iterated through by a base-element pointer. So in the above expression, *mat is converted to a pointer to the first element of the matrix mat[0][0].
*mat is equivalent to any of the following expressions:
&mat[0][0] = &(*(mat[0] + 0)) = mat[0] + 0 = mat[0] = *(mat + 0) = *(mat)
3. Using range-based for-loop
Another simple and elegant way to initialize a matrix is using a range-based for-loop (since C++11).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <algorithm> using namespace std; int main() { const size_t M = 5; const size_t N = 5; const int value = 1; int mat[M][N]; // don't forget to pass the reference `&` for (auto &row: mat) { for (auto &e: row) { e = value; } } // print the matrix return 0; } |
4. Using std::for_each function
This can also be done using the standard algorithm std::for_each, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <algorithm> using namespace std; int main() { const size_t M = 5; const size_t N = 5; int mat[M][N]; std::for_each(std::begin(mat), std::end(mat), [](auto &row) { std::fill(std::begin(row), std::end(row), 1); }); // print the matrix return 0; } |
Note that the global and static matrices are initialized with their default values when no initializer is specified. For instance, a global integer matrix will be zero-initialized by default.
That’s all about initializing a matrix in C++.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)