Skip to main content

Command Palette

Search for a command to run...

All about 2D Vectors

Published
2 min read
All about 2D Vectors
A

Android App Developer & Product Engineer @Mosaic Wellness. 4x Hackathon Winner. SWE Intern @Bonito Designs, @Legato Health Tech. Open for Freelance gigs. Documenting my tech journey via Hashnode & ranting about my life on Twitter.

In the previous articles, we saw about the basics of 1-D Vectors. Just like a 1-Dimensional vector, we have a 2-Dimensional vector as well. So, how is it different from the 1D vector?. Let's dive deep.

What is a 2D Vector?

  • 2D vector is a vector of a vector.
  • 2D vectors are often treated as a matrix with “rows” and “columns” inside it. Under the hood they are actually elements of the 2D vector.
  • All the properties that applies to a 1D vector is true for 2D vector as well.

2dvector.png

Use Case of a 2D Vector?

Well a 2D vector/ Matrix is one of the most important data structures. A variety of data can be stored in matrices. One example is:

  • To store the scores of 5 matches of 5 batsmen. Here we can use a 5x5 matrix where each row will be a batsman and the 5 columns in it will be his/her scores of a particular game.
  • A matrix is also used to represent a graph data structure. It is called as an adjacency matrix. We will cover it in detail in some other article.
#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector<vector<int>> vec1 = {
        {1,2,3,4,5},   // batsman 1
        {3,4,2,1,5},   // ...2
        {6,7,4,2,1},   // ...3
        {7,6,4,8,9},   // ...4
        {5,4,3,7,8}    // ..5
    };
}

Jojo Doge vs Cheems 05022022135101.jpg

General Syntax and Complexities of 2D Vector.

  • A 2D vector can be initialized in multiple ways. Just like the 1D vector, there are some useful inbuilt functions for a 2D vector as well. Talking about syntax, this is how you initialize a vector and perform some basic operations on it.
#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector<vector<int>>vec2;  // initializing a vector.
    vector<vector<int>>vec3(5,vector<int>(4,0)); // initializing a vector of size 5x4 with all 0 in it.
    vec2.push_back({1,2,3,4}); // pushing a vector inside a 2d vector.

    for(int i=0;i<5;i++)
    {
        for(int j=0;j<4;j++)
        {
            vec3[i][j]=i+j;   // another way to add elements to the 2d vector.
        }
    }
}

The four major operations that you will perform is searching, inserting, accessing, and deleting. The time complexities are as follows. *- Search: O(MN)

  • Insert at a particular position: O(M*N)
  • Push an element in the end: O(1)
  • Delete at a particular position: O(M*N)
  • Pop an element: O(1) **

    Conclusion

  • Well, this concludes the article. Now you have a good idea about the structure of a 2D vector, its time complexities and how it is different from 1D vector.
  • This article is enough to get you started with 2d vectors. Play around with it on your IDE and explore more by yourself.

More from this blog

C

CompSciWithIyush

105 posts