向量的合并向量
Merging vector of vectors
我的类是
class aMatrix {
private:
std::vector<std::vector<double> > mat;
unsigned rows;
unsigned cols; ...}
我想使用方法合并两个矩阵
aMatrix aMatrix::merge(const aMatrix& ob){
this->mat.reserve(this->mat.size()+ob.mat.size());
this->cols+=ob.cols;
this->mat.insert(this->mat.end(), ob.mat.begin(),ob.mat.end());
return *this;
}
但它不起作用。有什么想法如何让它工作吗?
编辑:没有错误。 它只是添加 ~0 列,然后添加与 ob.mat 不同的值。
编辑:
aMatrix mat1(3,3);
mat1(0, 0) = 1;
mat1(0, 1) = 1;
mat1(0, 2) = 1;
mat1(1, 0) = 2;
mat1(1, 1) = 3;
mat1(1, 2) = 5;
mat1(2, 0) = 11;
mat1(2, 1) = 0;
mat1(2, 2) = 5;
aMatrix ones(3,3);
ones(0,0)=1;
ones(1,1)=1;
ones(2,2)=1;
mat1.merge(ones);
结果是:
1, 1, 1, 1.11319e-308, 2, 3,
2, 3, 5, 1.11318e-308, 11, 0,
11, 0, 5, 1.11317e-308, 1, 0,
编辑:如何使其在这种符号中工作?
// Parameter Constructor
aMatrix::aMatrix(unsigned _rows, unsigned _cols, const double& _initial) {
mat.resize(_rows);
for (unsigned i = 0; i < mat.size(); i++) {
mat[i].resize(_cols, _initial);
}
rows = _rows;
cols = _cols;
}
编辑:好的,它现在可以工作了。 正确的代码:
aMatrix& aMatrix::merge(const aMatrix& ob){
this->mat.reserve(this->mat.size()+ob.mat.size());
this->cols+=ob.cols;
for (unsigned i = 0; i < this->mat.size(); i++) {
this->mat[i].insert(this->mat[i].end(), ob.mat[i].begin(),ob.mat[i].end() );
}
return *this;
}
谢谢:)
您应该逐行进行合并。您在代码中所做的是向原始矩阵添加更多行。您要做的是向原始矩阵添加更多列。
相关文章:
- 将向量的 N 段合并到位C++
- 使用向量在 c++ 中合并排序实现
- 如何合并函数返回的向量?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- C++ 合并排序返回原始向量
- 如何修复错误:使用向量查找最小值和最大值进行合并排序
- C++ 使用向量实现合并排序
- 如何使用C++有效地合并排序与向量
- 使用 move::语义将大量向量合并为更大的向量
- 合并排序的合并操作无法使用 c++ 向量
- 如何在C 中合并两个向量
- 合并,分类和删除重复分类的向量和未分类的向量
- 使用STL算法合并2个向量
- 合并2个不同长度的向量并事先对它们进行排序,而无需排序函数
- 将函数两个向量合并为第三个向量
- 如何在C++中将排序的向量合并为单个向量
- 与C 中的向量合并
- 将两个排序向量合并到一个排序向量中
- 将子向量合并/展平为单个向量c++(将2d转换为1d)
- 将向量合并到现有向量中