This article will explore how to initialize a two-dimensional vector with a given default value in C++.
In C++, we can define a two-dimensional vector of ints as follows:
1 |
std::vector<std::vector<int>> v; |
It results in an empty two-dimensional vector. To use it, we have to define the vector size and allocate storage for its elements. There are several methods to grow a two-dimensional vector with the help of the fill constructor, resize()
or push_back()
methods, or using initializer lists.
1. Using Fill Constructor
The recommended approach is to use a fill constructor to initialize a two-dimensional vector. The fill constructor creates a vector of the specified number of elements and fills with the specified value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #define M 3 #define N 4 int main() { // specify the default value to fill the vector elements int default_value = 1; // Using the fill constructor to initialize a two-dimensional vector // with a given default value std::vector<std::vector<int>> matrix(M, std::vector<int>(N, default_value)); // print the two-dimensional vector return 0; } |
We can split the above initialization into two parts – first initialize a vector of ints with a given default value and then use it to initialize the two-dimensional vector. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #define M 3 #define N 4 int main() { // specify the default value to fill the vector elements int default_value = 1; // first, initialize a vector of ints with a given default value std::vector<int> v(N, default_value); // Use the above vector to initialize the two-dimensional vector // using the fill constructor std::vector<std::vector<int>> matrix(M, v); // print the two-dimensional vector return 0; } |
2. Using resize()
function
The resize()
function is used to resize a vector to the specified size. We can use it to initialize a two-dimensional vector with a given default value, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #define M 3 #define N 4 int main() { // specify the default value to fill the vector elements int default_value = 1; // instantiate a vector of `M` objects of type `std::vector<int>` // and resize each object to size `N` with the given default value std::vector<std::vector<int>> matrix(M); for (int i = 0 ; i < M ; i++) { matrix[i].resize(N, default_value); } // print the two-dimensional vector return 0; } |
Here’s an alternative version of the above code, which uses an overloaded version of the resize()
function, which accepts the container size, and the object to be copied in that container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #define M 3 #define N 4 int main() { // specify the default value to fill the vector elements int default_value = 1; // instantiate vector object of type std::vector<int> std::vector<std::vector<int>> matrix; // resize the vector to `M` elements of type std::vector<int>, // each having size `N` and default value matrix.resize(M, std::vector<int>(N, default_value)); // print the two-dimensional vector return 0; } |
3. Using push_back()
function
Another plausible way of initializing a two-dimensional vector is to use the push_back()
function, which adds the given element at the end of a vector. The following C++ program demonstrates it:
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 26 27 28 29 30 |
#include <iostream> #include <vector> #define M 3 #define N 4 int main() { // specify the default value to fill the vector elements int default_value = 1; // instantiate vector object of type `std::vector<int>` and // use `push_back()` function to resize it std::vector<std::vector<int>> matrix; for (int i = 0; i < M; i++) { // construct a vector of ints with the given default value std::vector<int> v; for (int j = 0; j < N; j++) { v.push_back(default_value); } // push back above one-dimensional vector matrix.push_back(v); } // print the two-dimensional vector return 0; } |
Note when dimensions M
and N
are large, the above code suffers from potential performance penalties caused by frequent reallocation of memory by the push_back()
function. So, push_back()
should be used only when vector dimensions are not known in advance.
4. Using Initializer Lists
Finally, we can use initializer lists to initialize a two-dimensional vector with a given default value, as shown below. Note this will only work with C++11 and above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { // using an initializer list to initialize a two-dimensional vector std::vector<std::vector<int>> matrix { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } }; // print the two-dimensional vector return 0; } |
How to print the two-dimensional vector?
The following procedure would display a two-dimensional vector of integers using nested loops:
1 2 3 4 5 6 7 8 9 10 |
template<class T> void printVector(std::vector<std::vector<T>> const &matrix) { for (std::vector<T> row: matrix) { for (T val: row) { std::cout << val << " "; } std::cout << std::endl; } } |
That’s all about initializing a two-dimensional vector in C++.