2D vector in C++ with user defined size

A 2D vector is a vector of vector. Like 2D arrays, we can declare and assign values to 2D matrix

// C++ code to demonstrate 2D vector
#include <iostream>
#include <vector> // for 2D vector
using namespace std;

int main()
{
    // Initializing 2D vector "vect" with
    // values
    vector<vector<int> > vect{ { 1, 2, 3 },
                               { 4, 5, 6 },
                               { 7, 8, 9 } };

    // Displaying the 2D vector
    for (int i = 0; i < vect.size(); i++) {
        for (int j = 0; j < vect[i].size(); j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }

    return 0;
}

Outout :

1 2 3 
4 5 6 
7 8 9 

Like Java’s jagged arrays, each row of 2D vector can contain different number of columns.

// C++ code to demonstrate 2D vector where
// each row is of different size.
#include <iostream>
#include <vector> // for 2D vector
using namespace std;

int main()
{
    // Initializing 2D vector "vect" with
    // different number of values in each
    // row.
    vector<vector<int> > vect{ { 1, 2 },
                               { 4, 5, 6 },
                               { 7, 8, 9, 10 } };

    // Displaying the 2D vector
    for (int i = 0; i < vect.size(); i++) {
        for (int j = 0; j < vect[i].size(); j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }

    return 0;
}

Output :

1 2 
4 5 6 
7 8 9 10 

Exercise Problem : Define the 2D vector with different sizes of column input by user.
Examples:

Input : Number of rows : 5 
        Number of columns in rows : 
        2 3 4 5 1
Output : 1 2
         1 2 3
         1 2 3 4
         1 2 3 4 5 
         1

Input : Number of rows : 3
        Number of columns in rows : 
        3 2 1
Input : 3 3 2 1
Output : 1 2 3
         1 2
         1

First we take the input of row and and take the input of column of every row.
Now the initialize the memory of every row by the size of column.

// CPP program to create a 2D vector where 
// every row has 
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // size of row
    int row = 5;
    int colom[] = { 5, 3, 4, 2, 1 };

    // Create a vector of vector with size
    // equal to row.
    vector<vector<int> > vec(row); 

    for (int i = 0; i < row; i++) {

        // size of column
        int col; 
        col = colom[i];

        // declare  the i-th row to size of column
        vec[i] = vector<int>(col);
        for (int j = 0; j < col; j++)
            vec[i][j] = j + 1;
    }

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < vec[i].size(); j++)
            cout << vec[i][j] << " ";
        cout << endl;
    }
}

Output:

1 2 3 4 5
1 2 3 
1 2 3 4
1 2
1

This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 4 vote(s)