//一个小函数,精巧之处在于只分配两次内存,用下标计算完成行列转换 #include <iostream> using namespace std; template<class T> void get2array(T ***ptr, int row, int column) { *ptr=new T *[row]; (*ptr)[0]=new T[row*column]; for(int i=1;i<row; ++i) (*ptr)[i]=(*ptr)[i-1]+column; } int main() { int row = 8; int column = 10; int **ptr; get2array(&ptr,row,column); for(int i=0;i<row;++i) { for(int j=0;j<column;++j) { ptr[i][j] = i*column + j; std::cout << ptr[i][j] << " "; } std::cout << ‘\n‘; } std::cout << "行列转换" << std::endl; for(int i=0;i<column;++i) { for(int j=0;j<row;++j) { std::cout << ptr[j][i] << " "; } std::cout << ‘\n‘; } delete []ptr[0]; delete []ptr; return 0; }