将2D数组传递给函数 [英] Passing 2D Arrays to functions
问题描述
嘿,我有一个简短的问题.我正在尝试采用我编写的一堆代码,然后复制/粘贴10次..并将其转换为函数.我仍然在努力传递2D数组和指针.我有通过以下方式传递 square 2D数组的函数:
void createTrans( double orig [ double (* rot)[ double (* transform)[ 4 ]){
for ( int i = 0 ; i< ; 3; i ++){
for ( int j = 0 ; j< ; 3; j ++){
transform [i] [j] = rot [i] [j];
如果(j == 2 ){
transform [i] [j + 1] = orig [i];
}
}
}
transform [ 3 ] [ 3 ] = 1 ; transform [ 3 ] [ 0 ] = transform [ 3 ] [ 1 ] = transform [ 3 ] [ 2 ] = 0 ;
}
现在,这是我要制作成函数的代码:
double LG[100][66]; ifstream LGFile("mtlength\\LG.txt", ios::in); if (LGFile.is_open()) { stringstream iss, isn; string line, word; i=0; j=0; while (getline(LGFile,line)) { isn.clear(); isn << line; while (getline(isn,word,''\t'')) { if(j==(66)) {break;} LG[i][j]=atof(word.c_str()); j++; } j=0; i++; isn.clear(); } } LGFile.close();
所以我的问题很简单.如何将我的LG矩阵(n x m)传递给函数作为指针,可以通过文本文件填充它.也许我只是假设double (*rot)[3]
声明指定了3x3,但没有.
如果在编译时不知道矩阵的大小,那么就不能.解决方案
br/>
建议您声明一个简单的Matrix类,而不是处理2D数组:
class 矩阵 { 公共: // 此矩阵的行数 int Rows() const {返回 m_rows ; } // 此矩阵的列数 int Columns() const {返回 m_columns ; } // 直接访问内部数据:请谨慎使用! double * Data(){ return m_data; } // 构造一个空矩阵 矩阵() { m_data = NULL; m_rows = 0 ; m_columns = 0 ; } // 复制构造器 矩阵( const 矩阵和矩阵) { // 如果矩阵不为空,则将其克隆 如果(mat.Rows()!= 0 && mat.Columns()!= 0 ) { Alloc(mat.Rows(),mat.Columns()); memcpy(m_data,mat.Data(),m_rows * m_columns * sizeof ( double ))); } } // 清除此矩阵 〜矩阵() { 自由(); } // 为此矩阵分配内存 无效 Alloc( int 行, int 列) { 自由(); m_data = 新 [行*列]; m_rows =行; m_columns =列; } // 释放内存 无效 Free() { 如果(m_data!= NULL) 删除 [] m_data; m_data = NULL; m_rows = 0 ; m_columns = 0 ; } // 读取第k个元素 double 运算符 []( int k) 常量 {返回 m_data [k]; } // 写入第k个元素 双重& 运算符 []( int k){返回 m_values [k]; } // 读取第i行和第j列的元素 double 运算符()( int i, int j) const { return m_data [ i * m_columns + j]; } // 在第i行和第j列写入元素 双重& 运算符()( int i, int j){返回 m_data [i * m_columns + j]; } 私有: int m_rows,m_columns; double * m_data; };
然后,不要忘记通过引用(或指针)将其传递给任何函数,否则复制构造函数会减慢整个过程.或者,您可以只删除copy-constructor实现(但也可以删除析构函数,以避免两次删除同一指针!)
-----------------
您还可以使用模板:
template< int SIZE> void func( double (* rot)[SIZE]) { }
但是我仍然认为Matrix类是更好的解决方案.
Hey guys, I had a quick question. I''m am trying to take a bunch of code I wrote and then copy/pasted 10 times.. and turn it into a function. Passing 2D arrays and pointers is still something I''m struggled with. I have functions that pass square 2D arrays in the following way:
void createTrans(double orig[3], double (*rot)[3], double (*transform)[4]){ for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ transform[i][j] = rot[i][j]; if (j==2) { transform[i][j+1] = orig[i]; } } } transform[3][3]=1; transform[3][0]=transform[3][1]=transform[3][2]=0; }
Now here is the code I am trying to make into a function:
double LG[100][66]; ifstream LGFile("mtlength\\LG.txt", ios::in); if (LGFile.is_open()) { stringstream iss, isn; string line, word; i=0; j=0; while (getline(LGFile,line)) { isn.clear(); isn << line; while (getline(isn,word,''\t'')) { if(j==(66)) {break;} LG[i][j]=atof(word.c_str()); j++; } j=0; i++; isn.clear(); } } LGFile.close();
So my question is fairly simple. How do I pass my LG matrix (of n x m) to a function as a pointer where it can be filled via the text file. Maybe I am just assuming that thedouble (*rot)[3]
declaration specifies a 3x3 but it doesn''t. Any help on this would be much appreciated.解决方案If you don''t know the size of your matrix at compile time, then you can''t.
Instead of dealing with 2D arrays, I suggest you declare a simple Matrix class:
class Matrix { public: //number of rows of this matrix int Rows() const { return m_rows; } //number of columns of this matrix int Columns() const { return m_columns; } //direct access to the internal data: use with caution!! double* Data() { return m_data; } //constructs an empty matrix Matrix() { m_data = NULL; m_rows = 0; m_columns = 0; } //copy-constructor Matrix(const Matrix& mat) { //clones the matrix if it is not empty if (mat.Rows() != 0 && mat.Columns() != 0) { Alloc(mat.Rows(), mat.Columns()); memcpy(m_data, mat.Data(), m_rows * m_columns * sizeof(double)); } } //cleans this matrix ~Matrix() { Free(); } //allocates memory for this matrix void Alloc(int rows, int columns) { Free(); m_data = new[rows * columns]; m_rows = rows; m_columns = columns; } //frees the memory void Free() { if (m_data != NULL) delete [] m_data; m_data = NULL; m_rows = 0; m_columns = 0; } //reads the k-th element double operator[](int k) const { return m_data[k]; } //writes the k-th element double& operator[](int k) { return m_values[k]; } //reads the element at line i and column j double operator()(int i, int j) const { return m_data[i * m_columns + j]; } //writes the element at line i and column j double& operator()(int i, int j) { return m_data[i * m_columns + j]; } private: int m_rows, m_columns; double* m_data; };
Then, don''t forget to pass it by reference (or pointers) to any function, otherwise the copy-constructor will slow down the whole process. Or you can just remove the copy-constructor implementation (but also the destructor to avoid deleting the same pointer twice!)
-----------------
You can also use templates:
template<int SIZE> void func(double (*rot)[SIZE]) { }
But I still think the Matrix class is a better solution.
这篇关于将2D数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!