Initialization of a vector of vectors?

后端 未结 4 1945
孤街浪徒
孤街浪徒 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 06:54

    For the single vector you can use following:

    typedef int type;
    type elements[] = {0,1,2,3,4,5,6,7,8,9};
    vector vec(elements, elements + sizeof(elements) / sizeof(type) );
    

    Based on that you could use following:

    type matrix[2][2]=
    {
       {1,0},{0,1}
    };
    
    vector row_0_vec(matrix[0], matrix[0] + sizeof(matrix[0]) / sizeof(type) );
    
    vector row_1_vec(matrix[1], matrix[1] + sizeof(matrix[1]) / sizeof(type) );
    
    vector > vectorMatrix;
    vectorMatrix.push_back(row_0_vec);
    vectorMatrix.push_back(row_1_vec);
    

    In c++0x, you be able to initialize standard containers in a same way as arrays.

提交回复
热议问题