017:二维数组类 //有待修改或者去除重复版本
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 class Array2 { 6 private: 7 int** arr; 8 int row,col; 9 public: 10 Array2(int i = 0,int j = 0):row(i),col(j){ 11 arr = new int*[row]; 12 for(int k = 0;k < row;++ k) 13 arr[k] = new int[col]; 14 } 15 16 ~Array2(){ 17 for(int i = 0;i < row;++ i) 18 delete []arr[i]; 19 delete []arr; 20 } 21 22 int* operator[](const int& i){ 23 return arr[i]; 24 } 25 26 int& operator()(const int& i,const int& j){ 27 return arr[i][j]; 28 } 29 30 Array2& operator=(Array2& a){ 31 if(arr){ 32 for(int i = 0;i < row;++ i) 33 delete []arr[i]; 34 delete []arr; 35 } 36 row = a.row; 37 col = a.col; 38 arr = new int*[row]; 39 for(int i = 0;i < row;++ i) 40 arr[i] = new int[col]; 41 for(int i = 0;i < row;++ i) 42 for(int j = 0;j < col;++ j) 43 (*this)[i][j] = a[i][j]; 44 return *this; 45 } 46 }; 47 48 int main() { 49 Array2 a(3,4); 50 int i,j; 51 for( i = 0;i < 3; ++i ) 52 for( j = 0; j < 4; j ++ ) 53 a[i][j] = i * 4 + j; 54 for( i = 0;i < 3; ++i ) { 55 for( j = 0; j < 4; j ++ ) { 56 cout << a(i,j) << ","; 57 } 58 cout << endl; 59 } 60 cout << "next" << endl; 61 Array2 b; b = a; 62 for( i = 0;i < 3; ++i ) { 63 for( j = 0; j < 4; j ++ ) { 64 cout << b[i][j] << ","; 65 } 66 cout << endl; 67 } 68 return 0; 69 }
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 class Array2 { 6 private: 7 int** arr; 8 int row,col; 9 public: 10 Array2(int i = 0,int j = 0):row(i),col(j){ 11 arr = new int*[row]; 12 for(int k = 0;k < row;++ k) 13 arr[k] = new int[col]; 14 } 15 16 ~Array2(){ 17 for(int i = 0;i < row;++ i) 18 delete []arr[i]; 19 delete []arr; 20 } 21 22 int* operator[](const int& i){ 23 return arr[i]; 24 } 25 26 int& operator()(const int& i,const int& j){ 27 return arr[i][j]; 28 } 29 30 Array2& operator=(Array2& a){ 31 if(arr){ 32 for(int i = 0;i < row;++ i) 33 delete []arr[i]; 34 delete []arr; 35 } 36 row = a.row; 37 col = a.col; 38 arr = new int*[row]; 39 for(int i = 0;i < row;++ i) 40 arr[i] = new int[col]; 41 for(int i = 0;i < row;++ i) 42 for(int j = 0;j < col;++ j) 43 (*this)(i,j) = a(i,j); 44 return *this; 45 } 46 }; 47 48 int main() { 49 Array2 a(3,4); 50 int i,j; 51 for( i = 0;i < 3; ++i ) 52 for( j = 0; j < 4; j ++ ) 53 a[i][j] = i * 4 + j; 54 for( i = 0;i < 3; ++i ) { 55 for( j = 0; j < 4; j ++ ) { 56 cout << a(i,j) << ","; 57 } 58 cout << endl; 59 } 60 cout << "next" << endl; 61 Array2 b; b = a; 62 for( i = 0;i < 3; ++i ) { 63 for( j = 0; j < 4; j ++ ) { 64 cout << b[i][j] << ","; 65 } 66 cout << endl; 67 } 68 return 0; 69 }
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 class Array2 { 6 private: 7 int** arr; 8 int row,col; 9 public: 10 Array2(int i = 0,int j = 0):row(i),col(j){ 11 arr = new int*[row]; 12 for(int k = 0;k < row;++ k) 13 arr[k] = new int[col]; 14 } 15 16 ~Array2(){ 17 delete []arr; 18 } 19 20 int* operator[](const int& i){ 21 return arr[i]; 22 } 23 24 int& operator()(const int& i,const int& j){ 25 return arr[i][j]; 26 } 27 28 Array2& operator=(Array2& a){ 29 if(arr){ 30 for(int i = 0;i < row;++ i) 31 delete []arr[i]; 32 delete []arr; 33 } 34 row = a.row; 35 col = a.col; 36 arr = new int*[row]; 37 for(int i = 0;i < row;++ i) 38 arr[i] = new int[col]; 39 memcpy(arr,a.arr,sizeof(int)*row*col); 40 return *this; 41 } 42 }; 43 44 int main() { 45 Array2 a(3,4); 46 int i,j; 47 for( i = 0;i < 3; ++i ) 48 for( j = 0; j < 4; j ++ ) 49 a[i][j] = i * 4 + j; 50 for( i = 0;i < 3; ++i ) { 51 for( j = 0; j < 4; j ++ ) { 52 cout << a(i,j) << ","; 53 } 54 cout << endl; 55 } 56 cout << "next" << endl; 57 Array2 b; b = a; 58 for( i = 0;i < 3; ++i ) { 59 for( j = 0; j < 4; j ++ ) { 60 cout << b[i][j] << ","; 61 } 62 cout << endl; 63 } 64 return 0; 65 }