我试图创建一个X指针数组,引用尺寸为Y的矩阵16.有没有办法在不使用三指针的情况下在C ++中实现这一点?
编辑:为问题添加一些上下文。
屏幕上有许多几何图形,每个几何图形都有一个平面化为1x16阵列的变换。每个快照代表每个组件的转换。因此,num_snapshots的num_components是16的矩阵维度,其中后两个维度在运行时是已知的。最后,我们有许多应用了运动的几何形状。
我正在创建一个带有三指针参数的函数,尽管在我的情况下我不能使用三指针。我可以通过哪些其他方式传递此数据(可能通过多个参数)?最糟糕的情况是,我想将整个3D矩阵展平为一个阵列,尽管这似乎是一件草率的事情。有更好的建议吗?
我现在拥有的:
function(..., double ***snapshot_transforms, ...)
我想要完成的事情:
function (..., <1+ non-triple pointer parameters>, ...)
下面不是我创建的带有三重指针的函数,而是显示数据的全部内容。
static double ***snapshot_transforms_function (int num_snapshots, int num_geometries)
{
double component_transform[16];
double ***snapshot_transforms = new double**[num_snapshots];
for (int i = 0; i < num_snapshots; i++)
{
snapshot_transforms[i] = new double*[num_geometries];
for (int j = 0; j < num_geometries; j++)
{
snapshot_transforms[i][j] = new double[16];
// 4x4 transform put into a 1x16 array with dummy values for each component for each snapshot
for (int k = 0; k < 16; k++)
snapshot_transforms[i][j][k] = k;
}
}
return snapshot_transforms;
}
Edit2:我不能创建新的类,也不能使用像std这样的C ++特性,因为头文件中的公开函数原型被放入包装器(不知道如何解释三指针)以便翻译成其他语言。
编辑3:在评论中的每个人输入后,我认为使用扁平数组可能是最好的解决方案。我希望有一些方法来分割这个三重指针,并使用简单的数据类型(包括单指针)整齐地组织这些复杂的数据。虽然我不认为有一个很好的方法可以做到这一点,因为我的注意事项。我感谢大家的帮助=)
使用std::vector
更容易,更好,更不容易出错。毕竟你使用的是C ++而不是C.我用vector
s替换了所有的C风格的数组指针。 typedef doublecube
使得你不必一遍又一遍地输入vector<vector<vector<double>>>
。除此之外,代码基本上与您的代码保持一致。
如果你实际上不需要虚拟值,我会完全删除最里面的k
循环。 reserve
将保留真实数据所需的内存空间。
#include <vector>
using std::vector; // so we can just call it "vector"
typedef vector<vector<vector<double>>> doublecube;
static doublecube snapshot_transforms_function (int num_snapshots, int num_geometries)
{
// I deleted component_transform. It was never used
doublecube snapshot_transforms;
snapshot_transforms.reserve(num_snapshots);
for (int i = 0; i < num_snapshots; i++)
{
snapshot_transforms.at(i).reserve(num_geometries);
for (int j = 0; j < num_geometries; j++)
{
snapshot_transforms.at(i).at(j).reserve(16);
// 4x4 transform put into a 1x16 array with dummy values for each component for each snapshot
for (int k = 0; k < 16; k++)
snapshot_transforms.at(i).at(j).at(k) = k;
}
}
return snapshot_transforms;
}
添加一些面向对象通常会使代码更易于管理 - 例如,这里有一些代码可以创建一个包含每个Matrix不同行数的100个Matrix
对象的数组。 (如果你愿意的话,你也可以改变每个Matrix
中的列数,但我把它们留在了16):
#include <vector>
#include <memory> // for shared_ptr (not strictly necessary, but used in main() to avoid unnecessarily copying of Matrix objects)
/** Represents a (numRows x numCols) 2D matrix of doubles */
class Matrix
{
public:
// constructor
Matrix(int numRows = 0, int numCols = 0)
: _numRows(numRows)
, _numCols(numCols)
{
_values.resize(_numRows*_numCols);
std::fill(_values.begin(), _values.end(), 0.0f);
}
// copy constructor
Matrix(const Matrix & rhs)
: _numRows(rhs._numRows)
, _numCols(rhs._numCols)
{
_values.resize(_numRows*_numCols);
std::fill(_values.begin(), _values.end(), 0.0f);
}
/** Returns the value at (row/col) */
double get(int row, int col) const {return _values[(row*_numCols)+col];}
/** Sets the value at (row/col) to the specified value */
double set(int row, int col, double val) {return _values[(row*_numCols)+col] = val;}
/** Assignment operator */
Matrix & operator = (const Matrix & rhs)
{
_numRows = rhs._numRows;
_numCols = rhs._numCols;
_values = rhs._values;
return *this;
}
private:
int _numRows;
int _numCols;
std::vector<double> _values;
};
int main(int, char **)
{
const int numCols = 16;
std::vector< std::shared_ptr<Matrix> > matrixList;
for (int i=0; i<100; i++) matrixList.push_back(std::make_shared<Matrix>(i, numCols));
return 0;
}