zoukankan      html  css  js  c++  java
  • 传递二维数组

    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];
    }
    

      

  • 相关阅读:
    交互题
    线段树
    最小生成树
    拓扑排序
    欧拉回路
    RMQ问题
    dfs序与求子树子节点(染了色)的个数
    dp题
    树状数组与离散化
    没做完的题
  • 原文地址:https://www.cnblogs.com/kazama/p/11791472.html
Copyright © 2011-2022 走看看