Initialize 2D vector in C++ in different ways

C++ STL | Initializing a 2D Vector: In this article, we are going to see how to initialize the 2D vector in C++ in different ways with examples?
Submitted by Radib Kar, on July 09, 2020

Prerequisite: Initialize 1D vector

Before discussing about the initialization techniques let us state what a 2D vector is. A 2D vector in simple sense is a matrix having rows and column. In other words, a 2D vector is a vector of 1D vector, i.e., a vector having elements as 1D vector.

So what will be notation of 2D array?

vector<T> arr, where T is vector<W> where, W can be any datatype like int, char etc.

So a 2D integer vector we will define as vector<vector<int>> arr

Now let's get back to the point about initializing the 2D vector.

1) Initializing an empty 2D vector and then pushing back 1D arrays iteratively

This is the most naïve approach to initialize a 2D vector. Firstly, we just define an empty 2D vector. At that point, it has no idea about how many elements it's going to have. Then by using push_back() function we can simply keep adding 1D vectors at the back as per requirement. Now to add 1D vector we need to initialize that 1D arrays properly.

Below is an example to add elements as per user wants.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //empty 2Dvector initialized
    vector<vector<int> > two_D_vector;

    //below is an empty 1D vector
    vector<int> one_D_vector(5, 2);

    //pushing back the above 1D vector to the 
    //empty 2D vector each time
    for (int i = 0; i < 5; i++) {
        two_D_vector.push_back(one_D_vector);
    }

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (auto ij : it) {
            cout << ij << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

printing the 2D vector
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2

2) Initialize the vector with user defined size

We can initialize the 2D vector with user-defined size also. It's quite similar like creating a 2D dynamic array using malloc() or new operator. So say we want to initialize a 2D vector to rows n, and column m, then we need to initialize an n size 2D vector with elements of m size 1D vector. We can do that just like below, by default all the valued in the 2D array gets initialized as 0.

As we said earlier a 2D vector is a vector of a 1D vector. So for the upper use case, let's think exactly similarly as of 1D vector.

So the outer vector has size n(number of rows)

Let's define that,

vector<T> arr(n);

Now T is itself a 1D vector and has size m

Thus the element of the outer vector would vector<int>(m)

This combining,

vector<vector<int>> arr(n, vector<int>(m))
#include <bits/stdc++.h>
using namespace std;

int main()
{
    //n =no of rows
    //m =no of columns
    //both will be user defined
    int n, m;
    
    cout << "Enter number of rows, n\n";
    cin >> n;
    cout << "Enter number of columns, m\n";
    cin >> m;
    
    //2D vector initialized with user defined size
    vector<vector<int> > two_D_vector(n, vector<int>(m));

    //by default all values are 0
    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (auto ij : it) {
            cout << ij << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter number of rows, n
6
Enter number of columns, m
3
printing the 2D vector
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0

3) Initialize with user defined size and user defined element

Here instead of initializing with default 0, we initialize with a user-defined value. The method will be similar to the method of a 1D vector.

So for the outer vector,

vector<T> arr(T,W)

Where, W will be the user-defined element. Now here element is itself a 1D vector.

Thus the example will be like below,

Code 1:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //n=no of rows which is user defined
    int n;
    
    cout << "Enter number of rows, n\n";
    cin >> n;

    //user defined 1D array
    vector<int> one_D_vector{ 1, 2, 3 };

    //2D vector initialized with user defined size,
    //user defined element
    vector<vector<int> > two_D_vector(n, one_D_vector);

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (auto ij : it) {
            cout << ij << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter number of rows, n
5
printing the 2D vector
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

Code 2:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //n=no of rows which is user defined
    int n, m;
    
    cout << "Enter number of rows, n\n";
    cin >> n;

    //user defined 1D array
    cout << "Define your 1D array which will be element\n";
    vector<int> one_D_vector;
    cout << "keep pushing numbers, press 0 to stop\n";
    cin >> m;
    while (m) {
        one_D_vector.push_back(m);
        cin >> m;
    }

    //2 Dvector initialized with user defined size,
    //user defined element
    vector<vector<int> > two_D_vector(n, one_D_vector);

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (auto ij : it) {
            cout << ij << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Define your 1D array which will be element
keep pushing numbers, press 0 to stop
3 4 5 0
printing the 2D vector
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5

4) Initialize the 2D vector with user defined elements

We can also initialize the vector with user-defined elements. The syntax would be:

vector<vector<int>> two_D_vector{comma separated 1D elements};

The example is below:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //initialize with user-defined elements
    vector<int> arr{ 1, 2, 3, 4, 5, -1, -2, 6 };
 
    cout << "Printing the vector...\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
 
    return 0;
}

Output:

Printing the vector...
1 2 3 4 5 -1 -2 6

5) Initializing a vector with elements of other vector

We can also initialize a vector using elements of another vector. The vector is passed as a constructor to initialize the new vector. This is a deep copy indeed.

The example is like below:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //2D vector initialized with user 
    //defined -elements only
    vector<vector<int> > two_D_vector{
        { 1, 2, 3 }, //comma separated lists
        { 5, 6, 7 },
        { 8, 9, 3 }
    };

    //printing the 2D vector
    cout << "printing the 2D vector\n";
    for (auto it : two_D_vector) {
        //it is now an 1D vector
        for (auto ij : it) {
            cout << ij << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

printing the 2D vector
1 2 3
5 6 7
8 9 3





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.