gpt4 book ai didi

c++ - vector vector 的初始化?

转载 作者:IT老高 更新时间:2023-10-28 22:14:10 25 4
gpt4 key购买 nike

有没有办法以与初始化矩阵相同的、快速的方式初始化 vector vector ?

typedef int type;

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

vector<vector<type> > vectorMatrix; //???

最佳答案

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

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

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

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);

c++0x ,您可以像初始化数组一样初始化标准容器。

关于c++ - vector vector 的初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3061247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com