传递二维数组

时间:2019-11-04
本文章向大家介绍传递二维数组,主要包括传递二维数组使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
int m[][4] = { 
		{0,0,0,1},
		{1,0,0,1},
		{0,0,1,0} };

	
Mtriple<int> t(3, 4, (int*)m);

  在传递任意行和列的二维数组时,可以采取在main函数中写成上述形式的方法

而头文件中写的函数要通过地址找到值

template<class DataType>
inline Mtriple<DataType>::Mtriple(int mrow,int mcol, DataType *m)
{    int cnt=0;
        this->mrow = mrow;
	this->mcol = mcol;
	for (int i = 0; i < mrow; i++) {
		for (int j = 0; j < mcol; j++) {
			if (*(m+i*mcol+j) != 0) {
                            this->data[++cnt].row = i+1;
			    this->data[cnt].col = j+1;
			    this->data[cnt].e = *(m + i * mcol + j);
			    cout << this->data[cnt].row << " " << this->data[cnt].col << " " << this->data[cnt].e << endl;
			}
		}
	}
	
}                

  当然固定行列的写法只需要main 函数里

fun(m);

函数里

void fun(int m[3][4]) {
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 4; j++)
			cout << m[i][j];
}

  

原文地址:https://www.cnblogs.com/kazama/p/11791472.html