保留for迴圈架構
採C++ STL vector
採用C++ operator overloading運算子重載
程式碼函數如下:
int N = 2; vector<double> row(N); void MatrixM(vector<vector<double>>& C, vector<vector<double>> A, vector<vector<double>> B) { for(int i=0; i<N; i++) for (int j = 0; j < N; j++) { C[i][j] = 0; for (int k = 0; k < N; k++) C[i][j] += A[i][k] * B[k][j]; } } void print(vector<vector<double>>& C) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << C[i][j] << "\t"; cout << "\n"; } } vector<vector<double>>operator* (vector<vector<double>> A, vector<vector<double>> B) { vector<vector<double>> C(N, row); MatrixM(C, A, B); return C; }
沒有留言:
張貼留言