Initialization of a vector of vectors?

后端 未结 4 1947
孤街浪徒
孤街浪徒 2020-12-15 06:17

Is there a way to initialize a vector of vectors in the same ,quick, manner as you initialize a matrix?

typedef int type;

type matrix[2][2]=
{
{1,0},{0,1}
         


        
4条回答
  •  臣服心动
    2020-12-15 07:00

    If the matrix is completely filled -

    vector< vector > TwoDVec(ROWS, vector(COLS));
    //Returns a matrix of dimensions ROWS*COLS with all elements as 0
    //Initialize as -
    TwoDVec[0][0] = 0;
    TwoDVec[0][1] = 1;
    ..
    .
    

    Update : I found there's a better way here

    else if there are variable number of elements in each row(not a matrix) -

    vector< vector > TwoDVec(ROWS);
    for(int i=0; i

提交回复
热议问题