In this article we will discuss different ways to initialize two dimensional in C++.

**Table of Contents**

- C++: Initialize 2D vector with hardcoded values using initializer list
- C++: Initialize 2D vector with same value
- Initialize 2D vector with different values in each row
- C++: Create an empty 2D vector and the resize to fill values
- C++: Initialize a 2D vector with a given range of numbers

To construct a matrix like structure in C++ i.e. a two dimensional vector, we need to create a vector of vectors. Parent vector will contain some nested vectors of same size. Each of the nested vector represents a row of the matrix, and number of elements in each nested vector represents the column values for that particular row. Let’s see few ways to initialize a two dimensional vector in C++.

## C++: Initialize 2D vector with hardcoded values using initializer list

If you want to initialize two dimensional vector or matrix with some hard coded values, then initializer list is the best suited for you. We will create a vector of integer vectors and initialize it with a initializer list of few initializer lists. For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { // Initializing a 2D Vector i.e. vector of vectors vector<vector<int>> matrix = { {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {5, 6, 8, 1, 12 }, {1, 7, 2, 4, 18 }, }; // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

Output:

**Advertisements**

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 8, 1, 12, 1, 7, 2, 4, 18,

## C++: Initialize 2D vector with same value

To construct a two dimensional vector like matrix, we can create a vector of vectors. For example, to create a matrix of 5 rows and 4 columns,

we can create a 5 vectors of size 4, each vector represent a row in matrix. Then we can add these 5 vectors to an another vector to have aÂ matrix like structure. For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int value = 10; int rows = 5; int columns = 4; // Initialize a vector of vector or 2D vector of size 5X4 with value 10 vector < vector<int> > matrix (rows, vector<int>(columns, value) ); // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

**Output**

10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,

It create a 2D vector with 5 rows and 4 columns, with same value 10.

## Initialize 2D vector with different values in each row

We can create a vector of empty vectors. After that resize the nested vector with different values. For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int value = 10; int rows = 5; int columns = 4; // Initialize aan empty vector of vector or 2D vector of 5 rows // Then initialize each row with different value by resizing. vector < vector<int> > matrix(rows); for (auto & row : matrix) { row.resize(columns, value); value++; } // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

**Output**

10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14,

Each of the nested vector represents a row. While resizing we set the different value in each row.

## C++: Create an empty 2D vector and the resize to fill values

We can create an empty vector of vector and then we can resize it later to fill default value. For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int value = 10; int rows = 5; int columns = 4; // instantiate vector object of type vector<int> vector<vector<int>> matrix; // resize the vector to 5 elements of type vector<int>, // each having size 4 and given default value 11 matrix.resize(rows, vector<int>(columns, 11)); // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

**Output**

11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,

## C++: Initialize a 2D vector with a given range of numbers

We can create an empty vector of vector and then add items one by one row wise. While adding we will increment the values. For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int rows = 5; int columns = 4; // instantiate vector object of type vector<int> vector<vector<int>> matrix (rows); int counter = 1; for (int r = 0; r < matrix.size(); r++) { for(int i = 0; i< columns; i++) { matrix[r].push_back( (r+1) * (i+1) ); } } // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

Output

1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16, 5, 10, 15, 20,

We first create a vector of 5 empty vectors. Then iterated over each of the empty nested vector and added 4 elements to each of them. So, in total we added 20 elements in 5 nested vectors to create a 2D matrix of size 5X4.

**Summary:**

We learned about the different ways to initialize two dimensional vector in C++ either with hardcoded values or values in a range.

**Advertisements**

Thanks for reading.