gpt4 book ai didi

c++ - 如何在 Constructor 中编辑 2D vector

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:30 25 4
gpt4 key购买 nike

感谢所有帮助,我已将初始化移至构造函数,但是,我在定义 2D vector 时遇到困难:

这是我所做的:

private:
vector < vector <int> > Matrix;
vector < vector <int> > temp_m;
vector <int> elements
string input;
int value;
function()
{
//Initialize Both Matrices (one which holds the puzzle and the
//other which holds the values between 1 and 9
//Create a vector of vectors:
for(int i = 0; i < 9; i++)
elements.push_back(i+1);
for(int i = 0; i < 9; i++)
Matrix[i].push_back(elements); //ERROR HERE
}

我在定义 2D 矩阵的行中遇到错误。我想将矩阵推回到它的索引中,因为它是矩阵的矩阵。

最佳答案

“行”的声明和构造不在同一个地方。构造属于初始化列表:

class MyClass
{
public:
MyClass::MyClass()
: row(9,0), elements(9)
{
}

private:
vector < vector <int> > Matrix;
vector < vector <int> > temp_m;
vector <int> row;
vector <int> elements;
string input;
int value;
}

如果您对成员变量有任何其他特殊的大小调整或初始化,则需要构造参数(例如上面的 Matrix 和 temp_e),它们也属于初始化列表。

关于c++ - 如何在 Constructor 中编辑 2D vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12610844/

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