#include <iostream> #include <array> using namespace std; //stack.h template<typename T> class Array { public: Array(int row,int col); T* operator[](int row); public: T* m_pT; int m_Row; int m_Col; }; template<typename T> Array<T>::Array(int row,int col) { m_Row = row; m_Col = col; m_pT = (T*)malloc(sizeof(T)*row*col); if (m_pT == NULL) return; for (int i = 0; i < row * col; i++) *(m_pT + i) = 0; } template<typename T> T* Array<T>::operator[](int row) { if (row < 0 || row >= m_Row) exit(OVERFLOW); return m_pT + row * m_Col; } bool Judge(Array<int> p) { int row = p.m_Row; int col = p.m_Col; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int temp = p[i][j]; for (int l = j + 1; j < col; j++) { if (temp == p[i][l]) return true; } for (int l = i + 1; l < row; l++) { for (int m = 0; m < col; m++) { if (temp == p[l][m]) return true; } } } } return false; } int main() { int row, col; cin >> row >> col; Array<int>p(row, col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cin >> p[i][j]; } } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << p[i][j] << " "; } cout << endl; } cout << p[row - 1][col - 1]<<endl; bool flag = Judge(p); if (flag == true) cout << "有重复的" << endl; else cout << "没有重复的" << endl; }
注意template的使用以及运算符的重载(返回值)
标签:pT,int,++,二维,重载,Array,col,模板,row From: https://www.cnblogs.com/saucerdish/p/17777924.html