矢量向量的初始化?

Eri*_*ric 17 c++ stl vector matrix

有没有办法在初始化矩阵时以相同,快速的方式初始化矢量矢量?

typedef int type;

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

vector<vector<type> > vectorMatrix;  //???
Run Code Online (Sandbox Code Playgroud)

sin*_*nek 7

对于单个矢量,您可以使用以下内容:

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

基于此,您可以使用以下内容:

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

vector<int> row_0_vec(matrix[0], matrix[0] + sizeof(matrix[0]) / sizeof(type) );

vector<int> row_1_vec(matrix[1], matrix[1] + sizeof(matrix[1]) / sizeof(type) );

vector<vector<type> > vectorMatrix;
vectorMatrix.push_back(row_0_vec);
vectorMatrix.push_back(row_1_vec);
Run Code Online (Sandbox Code Playgroud)

c ++ 0x中,您可以以与数组相同的方式初始化标准容器.