2D Vector Implementation in C++ Complete Guide

Are you looking for a tutorial and ways how you can implement and initialize 2D vectors in C++? In this tutorial, I will show you various ways you can initialize and implement 2D vectors in C++ easily.

As you saw in my earlier article where I showed you how you can initialize vector in c++ in 6 different ways. Similarly, you can initialize a 2D vector in 6 different ways to get started.

A two-dimensional vector is a vector within a vector. As with two-dimensional arrays, we can declare and assign values to a two-dimensional vector.

How to Initialize 2D Vector in C++? [Simple Way]

The most common way to initialize a 2D vector in C++ is shown below. Make sure that you include the vector.h library before initializing the vectors in C++.

Or including numerous different types of libraries for your code in C++, simply add the below-mentioned library in your code to minimize the effort.

#include<bits/stdc++.h>
vector<vector<int>> twoDimenVector;

The above line initializes the empty two-dimensional vector or in layman terms, it initializes a two-dimensional array that is completely empty.

Initiliazing 2D Vector with Some Values

If you want to initialize a 2D vector in C++ with some values then you need to add the code as shown in the below example.

#include<bits/stdc++.h>
using namespace std;
 
int main(){
  //Intialize Two Dimensional Vector with Some Values
    vector<vector<int>> twoDimenVector {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 
  
  //Printing Two Dimentional Vector in C++
    for(int i=0;i<twoDimenVector.size();i++){
        for(int j=0;j<twoDimenVector[i].size();j++)
            cout<<twoDimenVector[i][j]<<" ";
        cout<<endl;
    }
  return 0;
}

Output:

1 2 3 
4 5 6 
7 8 9 

“vector<vector<>>” is the symbol used to denote that you need a two-dimensional array or vector in your code. And values such as {1,2,3} is a vector that is inside another vector having all three vectors in it.

Initialize 2D Vector with Size and Value

If you are looking to specify a 2D vector with some size and all the elements to some fixed values initialize you can do that easily using Fill Constructor present in Vector Library of C++. Below is the code example of how you can do it.

#include<bits/stdc++.h>
using namespace std;
 
int main(){

    //Specifying Number of Columns in 2D Vector
    int numColToFill = 3;
 
    //Specifying Number of Rows in 2D Vector
    int numRowToFill = 3;
 
    // Initializing the Column first with Initial Value
    vector<int> row(numColToFill, 0);
 
    // Initializing the 2-D vector with Row and Column Values
    vector<vector<int>> twoDimenVector(numRowToFill, row) ;
 
    for(int i=0;i<twoDimenVector.size();i++){

        for(int j=0;j<twoDimenVector[i].size();j++)

            cout<<twoDimenVector[i][j]<<" ";

        cout<<endl;
    }
    return 0;                
}

Output:

0 0 0 
0 0 0 
0 0 0 

Using push_back

You can initialize the 2D vector by creating a one-dimensional vector and pushing all the one-dimensional vectors as elements to a two-dimensional array. You can push back empty as well as vectors with some values present in them.

Below is an example to show how you can push back a one-dimensional vector or array in a two-dimensional vector or array in C++.

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

void printTwoDimensionalArray(vector<vector<int>> &twoDimenVector){
    //Printing the Two Dimensional Array
    for(auto oneDim:twoDimenVector){
        for(auto elem:oneDim){
            cout<<elem<<" ";
        }
        cout<<endl;
    }
}
 
int main(){

    //Initializing Two Dimensional Vector (2D Vector)
    vector<vector<int>> twoDimenVector; 

    //Initializing 1D Vector
    vector<int> inputTo2DVector = {1,2,3,4};

    twoDimenVector.push_back(inputTo2DVector);

    printTwoDimensionalArray(twoDimenVector);

    int x = 5;
    //Now let us update values of inputTo2DVector
    for (int i = 0; i < inputTo2DVector.size(); i++)
    {
        inputTo2DVector[i] = x;
        x++;
    }

    cout<<endl<<"Different Output Starts From Here"<<endl;
    //Updating Two Dimensional Array and Printing it

    twoDimenVector.push_back(inputTo2DVector);

    printTwoDimensionalArray(twoDimenVector);

    return 0;         
}

Output:

1 2 3 4 

Different Output Starts From Here
1 2 3 4 
5 6 7 8 

Printing 2D Vector Using Iterator and Adding Element in 2D Vector

You can use Iterator inbuild in vector class to print all the elements of a two-dimensional vector. Below is the code example in which you can understand how to initialize an iterator for a two-dimensional vector and print the values.

Also, you can learn from the below code how to add the element or vector in a two-dimensional vector.

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

void printTwoDimensionalArray(vector<vector<int>> &twoDimenVector){
    //intializing iterators
    vector<vector<int>>::iterator twoDimIter;

    //Initiazing Iterator
    vector<int>::iterator oneDimIter;

    //Printing the Two Dimensional Vector
    for(twoDimIter=twoDimenVector.begin(); twoDimIter!=twoDimenVector.end(); ++twoDimIter){
        for(oneDimIter = twoDimIter->begin(); oneDimIter != twoDimIter->end(); ++oneDimIter){
            cout<<*oneDimIter<<" ";
        }
        cout<<endl;
    }
}
 
int main(){

    //Initializing Two Dimensional Vector (2D Vector)
    vector<vector<int>> twoDimenVector; 

    //Initializing 1D Vector
    vector<int> inputTo2DVector = {1,2,3,4};

    twoDimenVector.push_back(inputTo2DVector);

    printTwoDimensionalArray(twoDimenVector);

    return 0;         
}

Output:

2D Vector Implementation in C++ Complete Guide

Conclusion

Two-dimensional vectors are quite simple to utilize in C++. This type of vector is useful when dealing with matrices, graphs, and other two-dimensional objects.

That is all for this post on how to square something in C++. Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

Further Read:

  1. How To Square Something in C++? 3 Unique Ways
  2. Iterate Through Character Of String in C++

Subscribe to our newsletter!

Share your love

2 Comments

Leave a Reply