如何用c++中的指针初始化2D矢量
How to initialize a 2D vector with pointers in c++?
所以我试图在c++中初始化一个2D指针向量/指针矩阵(/3d向量?(。指向单元格对象的指针的二维矩阵。当运行代码时,我得到这个错误:
错误:没有用于调用的匹配函数'std::vectorstd::vector<单元格*>:push_back(std::vector&('
17|matrix.push_back(temp(;
注意:单元格是一个类
城市.hh:
std::vector <std::vector <Cell*>> matrix;
City(unsigned int M, unsigned int N);
City.cc:
#include "City.hh"
City::City(unsigned int N, unsigned int M)
{
// size of "matrix"
this->M = M;
this->N = N;
for(unsigned int i = 0; i < M; i++)
{
vector <Cell> temp;
for(unsigned int j = 0; j < N; j++)
{
Cell cell = Cell();
temp.push_back(cell);
}
matrix.push_back(temp);
}
}
我建议您避免使用指针来保持对象之间的距离(由于各种原因(,而是这样做:
std::vector< std::vector< Cell > > matrix( M, std::vector< Cell >( N ) );
相关文章:
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- 如何从initializer_list初始化 2D 数组
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 为什么不能用两层列表初始值设定项初始化 2D std::array?
- 如何从嵌套的 std::initializer_list 初始化 2D C 样式数组?
- 从另一个类C++的构造函数初始化 2D 数组
- 在构造函数中初始化 2D 数组
- 如何初始化2D向量C
- 在类的构造函数中初始化 2D 向量
- 初始化2D矢量
- 无法解决动态初始化 2D 数组的问题
- 使用随机值初始化 2D 向量
- 使用多个std ::数组初始化2D std ::数组
- 如何使用初始值设定项列表初始化 2D 向量成员
- 如何在标头中声明后初始化 2D 字符数组
- 无法在我的C++课中初始化 2d 矢量
- 如何在 c++ 中使用构造函数初始化 2d 向量
- 我正在尝试创建一个函数来初始化 2D 数组C++
- 用于初始化 2D std::array 成员的初始值设定项列表
- 在c++中使用1 d数组初始化2d数组