cpp

2D Vector in C++

bhavinvirani

BHAVIN VIRANI

Posted on June 11, 2021

2D Vector in C++

We can define 2D Vector as Vector of Vector followed by N number of rows where each of the row is individual a vector. in other words 2D vector is almost similar to a array of vectors but the difference is only in the dynamic properties.

Syntex

vector <vector<data_type>> vector_name;

  • vector <vector<int>> vct

Initializition

initialization of vector
Here vec is the vector of vectors. where outer vector vec represent row as 2D matrix and every sub vectors represent column elements of particular row vector.

limit vector initialization

  • N = Number of rows
  • M - Number of columns Here 2D vector contains N number sub vectors(row) and every sub vector has M number of elements vale of 0
// output of previous initialization 
{ 
  0 0 0 0 
  0 0 0 0
  0 0 0 0 
}
Enter fullscreen mode Exit fullscreen mode

Iteration on 2D vector

Iteration on 2D vector on 2D vector

  • vect.begin() – It returns an iterator to the first vector in a 2-D vector.
  • vect.end() – It returns an iterator to the end of the 2-D vector.
  • vect.size()– It returns number of elements in vector push and pop in vector
  • v.push_back – Add vector to the empty 2-D vector
  • v.pop_back() – Remove the last vector from a 2-D vector
💖 💪 🙅 🚩
bhavinvirani
BHAVIN VIRANI

Posted on June 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related