Initialization of a vector of vectors?

后端 未结 4 1943
孤街浪徒
孤街浪徒 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:01

    std::vector> vector_of_vectors;
    

    then if you want to add, you can use this procedure:

    vector_of_vectors.resize(#rows); //just changed the number of rows in the vector
    vector_of_vectors[row#].push_back(someInt); //this adds a column
    

    Or you can do something like this:

    std::vector myRow;
    myRow.push_back(someInt);
    vector_of_vectors.push_back(myRow);
    

    So, in your case, you should be able to say:

    vector_of_vectors.resize(2);
    vector_of_vectors[0].resize(2);
    vector_of_vectors[1].resize(2);
    for(int i=0; i < 2; i++)
     for(int j=0; j < 2; j++)
       vector_of_vectors[i][j] = yourInt;
    

提交回复
热议问题