嵌套向量的动态分配

Dynamic allocation of nested vectors

本文关键字:动态分配 向量 嵌套      更新时间:2023-10-16

我做了一些类似的事情:

Grid(int row, int col):num_of_row_(row), num_of_col_(col) {
     grid_ = new vector<vector<bool> > (row, col);  
}

其动态地分配嵌套向量。这是正确的吗?我的意思是使用以下语法:

new vector<vector<type> > (outersize, innersize)

其中**outersize和innersize都是"int"变量。**

更新:我实际上使用了这个代码,而且它很有效。我只是想找出原因。

传递给构造函数的第二个参数是要重复outersize次的向量的元素。您应该使用以下语法:
new vector<vector<type> > (outersize, vector<type>(innersize, elementValue));

例如,要使bool的50x25网格最初设置为true,请使用:

vector<vector<bool> > *grid = new vector<vector<bool> >(50, vector<bool>(25, true));