how to initialize a 2d vector and what will vector.size() then denote??row or coln??
2 d vector size() operator use
Hi @khushipriya681, to make a 2D vector follow the following syntax:
vector < vector < data_type > > vector_name ;
For ex to make a 2D vector of int named ‘v’ use:
vector < vector < int > > v ;
NOTE: Always remeber to keep the 2 '>'s in end apart by atleast 1 space because if you put them together it will mean something else.
Now here if you wright v.size() it will give you no. of rows.
Also remeber that you have created a vector of vectors and vector’s size is dynamic so for a 2d vector no. of columns of 2 vectors in it for ex:
v[i].size and v[j].size() provided i!=j
need not to be same.
Hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
how can we assign values in a 2d array using push_back() function??please give an example
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Hi @khushipriya681, a 2D vector is acually a vector of vectors of something.
So in general when we make a
vector < int > v ;
…
…
v.push_back(12) ;
that means v is a vector of ints and we can push ints in it, i.e. we are pushing that element inside vector v which it was asked to be made of.
Similarly, if we declare:
vector < vector < int > > v2d ;
, this means we are making a vector of vectors of ints.
Now , just like we were pushing int type of data in v, similarly we can push v type of materials in v2d. i.e.
vector < vector < int > > v2d ;
…
…
v2d.push_back(v) ;
Apologies for late reply. Hope this helps .