嵌套向量的动态分配
Dynamic allocation of nested vectors
我做了一些类似的事情:
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));
相关文章:
- C++ 将抽象类型的动态分配对象传递给函数并存储在向量中
- 动态分配特征向量的模因
- 代码超出范围.如何动态分配此向量?
- 在读取大型 txt 文件时动态分配结构内的向量
- 通过引用和动态分配传递向量
- 是否可以动态分配向量?
- 附加到具有非动态分配堆栈的向量
- 使用嵌套向量动态分配方阵
- 如何释放动态分配的向量
- 如何在C++中使用"new"动态分配向量数组?
- 如何创建指向动态分配对象的指针向量
- 你能动态分配一个向量吗?
- 在C++中动态分配向量的安全方法是什么
- 动态分配随机特征向量Xd
- 另一个向量与动态分配的数组
- 对象的 C++ 向量,包含动态分配的内存 - "擦除"不起作用
- C++:动态内存分配+向量
- 将动态分配的变量移动到向量中
- 动态分配向量
- 动态分配向量(或向量的向量)