如何在构造函数中编辑 2D 矢量
How can I edit a 2D vector in a Constructor
感谢您的所有帮助,我已经将初始化移至构造函数,但是,我在定义 2D 向量时遇到困难:
这是我所做的:
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 矩阵的行中遇到错误。我想将矩阵推回其索引,因为它是矩阵的矩阵。
"row"的声明和它的构造不在同一个地方。构造属于初始值设定项列表:
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;
}
如果您有任何其他特殊的成员变量大小调整或初始化,则需要构造参数(例如上面的矩阵和temp_e),它们也属于初始值设定项列表。
这是不合法的(无论如何,肯定是在 C++11 之前,C++11 中有变化,但我不确定确切的规则)。您可以改为在构造函数初始值设定项列表中指定它:
A::A() : row(9, 0), elements(9) {}
并更改为:
private:
vector<int> row;
vector<int> elements;
尝试从声明中删除(9 , 0)
。在 C++ 中,不能从类变量声明调用构造函数。您需要使用初始值设定项列表从类构造函数执行此操作。
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 编辑文件中的文本
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- SetDlgItemInt 不会更改嵌入资源的编辑框
- 双打分类/编辑问题的2D向量
- 如何在构造函数中编辑 2D 矢量
- 编辑2d数组中的元素范围
- 在2d数组中编辑值