Open In App

How to Initialize 2D Vector in C++?

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
1 2 3 
4 5 6 

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;
}

Output
5 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;
}

Output
5 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;
}

Output
1 2 3 
4 5 6 

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;
}





Next Article
Article Tags :
Practice Tags :

Similar Reads