How to Initialize 2D Vector in C++?
Last Updated :
22 Nov, 2024
Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.
The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method is convenient initializing the 2D vector with hardcoded values. Let's take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the 2D vector
vector<vector<int>> v = {{1, 2, 3},
{4, 5, 6}};
for (auto i : v) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Unlike 2D arrays, the nesting in the initializer list in this case cannot be skipped as it is used to determine the number of rows and columns.
Apart from the above method, C++ also provides other methods to initialize a 2D vector depending on the situation. Some of them are:
With User Defined Size and Default Value
A 2D vector can be initialized with a default row and column size with a default value for all elements during its declaration.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize 2D vector of size 3 x 3 with
// defualt value 5
vector<vector<int>> v(3, vector<int>(3, 5));
for (auto i : v) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output5 5 5 5
5 5 5 5
5 5 5 5
Using fill() Function
The fill() function can be used to initialize a 2D vector with a specific value but size need to be specified beforehand. This method is generally used to initialize a 2D vector of user defined size with a single value after declaration.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v(3, vector<int>(3));
// Initialize 2D vector with value 5
fill(v.begin(), v.end(), vector<int>(3, 5));
for (auto i : v) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output5 5 5 5
5 5 5 5
5 5 5 5
The fill() method initializes all rows with the same size. For non-uniform rows, you must initialize each row manually.
Inserting Rows One by One
A 2D vector can be initialized by inserting rows individually using the vector push_back() method. This is preferred when we want to initialize 2D vector with hardcoded values after initialization.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v;
// Insert rows into the 2D vector
v.push_back({1, 2, 3});
v.push_back({4, 5, 6});
for (auto i : v) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Using Another 2D Vector
A 2D vector can also be initialized by copying another 2D vector using the copy constructor.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> v1 = {{1, 2, 3},
{4, 5, 6}};
// Initialize 2D vector using v2
vector<vector<int>> v2(v1);
for (auto i : v2) {
for (auto j : i) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Similar Reads
How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me
4 min read
How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that
3 min read
How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look
2 min read
How to Resize a 2D Vector in C++? In C++, resizing the 2D vector means increasing or decreasing the row and column size of 2D vector. In this article, we will learn different methods to resize a 2D vector in C++.The most efficient way to resize the 2D vector is by using vector resize() function. Letâs take a look at a simple example
2 min read
How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector
2 min read