###実現したいこと
c++の勉強のため、行列クラスを作っています。行列クラスのオブジェクトに行ごと(列ごと)にアクセスするため、大きさ1×n (m×1)の部分行列の参照を返すメンバ関数を作りたいです。しかし、実装方法が全く分かりません・・・。何かいい方法はあるでしょうか?ご教示お願いします。
###ソースコード
Matrix.h
c++
1#ifndef _Matrix_H_ 2#define _Matrix_H_ 3 4#include <bits/stdc++.h> 5using namespace std; 6 7class Matrix { 8public: 9 Matrix(int r, int c);//コンストラクタで行列の大きさを決定 10 Matrix(); 11 Matrix(const Matrix& A); 12 13 vector<vector<double> > ele; //行列の要素はvector配列で管理 14 15 void set(double first, ...); //要素をセット 16 void show(); //行列を標準出力に表示 17 int rownumber() const; //行数を返す 18 int columnumber() const; //列数を返す 19 20 21 //今回実装したい関数 22 Matrix& row(int n); //引数の行の参照を返す 23 Matrix& colum(int n); //引数の列の参照を返す 24 25 26 27 //演算子オーバーロード 28 //添え字演算子 29 vector<double>& operator[](int n); 30 31 //代入演算子 32 Matrix& operator=(const Matrix& A); 33 34 //二項演算子 35 Matrix operator+(const Matrix& A) const; 36 Matrix operator-(const Matrix& A) const; 37 Matrix operator*(const Matrix& A) const; 38 39 //単項演算子 40 Matrix operator+() const; 41 Matrix operator-() const; 42 Matrix& operator+=(const Matrix& A); 43 Matrix& operator-=(const Matrix& A); 44 Matrix& operator*=(const Matrix& A); 45 Matrix& operator*=(double k); 46 Matrix& operator/=(double k); 47 48 //比較演算子 49 bool operator==(const Matrix& A) const; 50 bool operator!=(const Matrix& A) const; 51 52 53private: 54 int _row; //行数 55 int _colum; //列数 56}; 57 58//二項演算子 59Matrix operator*(double k, const Matrix& A); 60Matrix operator*(const Matrix& A, double k); 61Matrix operator/(const Matrix& A, double k); 62 63 64 65#endif 66 67
Matrix.cpp
c++
1#include "Matrix.h" 2 3Matrix::Matrix(int r, int c){ 4 _row = r, _colum = c; 5 ele.resize(_row); 6 for(int i = 0; i < _row; i++) { 7 ele[i].resize(_colum); 8 } 9 for(int i = 0; i < _row; i++) { 10 for(int j = 0; j < _colum; j++) { 11 ele[i][j] = 0;//0で初期化 12 } 13 } 14} 15 16Matrix::Matrix(){ 17 _row = 1, _colum = 1; 18 ele.resize(1); 19 ele[0].resize(1); 20 ele[0][0] = 0; 21} 22 23Matrix::Matrix(const Matrix& A){ 24 _row = A.rownumber(), _colum = A.columnumber(); 25 ele.resize(_row); 26 for(int i = 0; i < _row; i++) { 27 ele[i].resize(_colum); 28 } 29 for(int i = 0; i < _row; i++) { 30 for(int j = 0; j < _colum; j++) { 31 ele[i][j] = A.ele[i][j]; 32 } 33 } 34} 35 36 37void Matrix::set(double first, ...){ 38 double element = first; 39 va_list args; 40 va_start(args, first); 41 for(int i = 0; i < _row; i++) { 42 for(int j = 0; j < _colum; j++) { 43 ele[i][j] = element; 44 element = va_arg(args, double); 45 } 46 } 47 va_end(args); 48} 49 50void Matrix::show(){ 51 for(int i = 0; i < _row; i++) { 52 for(int j = 0; j < _colum; j++) { 53 printf("%lf ", ele[i][j]); 54 } 55 printf("\n"); 56 } 57 printf("\n"); 58} 59 60int Matrix::rownumber() const { 61 return _row; 62} 63 64int Matrix::columnumber() const { 65 return _colum; 66} 67 68 69//今回実装したい関数 70Matrix& Matrix::row(int n){ 71 72//参照を返せるようにしたい 73 74} 75 76//今回実装したい関数 77Matrix& Matrix::colum(int n){ 78 79//参照を返せるようにしたい 80 81} 82 83 84//演算子オーバーロード 85vector<double>& Matrix::operator[](int n) { 86 return ele[n]; 87} 88 89Matrix& Matrix::operator=(const Matrix& A){ 90 this->_row = A.rownumber(); 91 this->_colum = A.columnumber(); 92 this->ele.resize(_row); 93 for(int i = 0; i < _row; i++) { 94 this->ele[i].resize(_colum); 95 } 96 97 for(int i = 0; i < _row; i++) { 98 for(int j = 0; j < _colum; j++) { 99 this->ele[i][j] = A.ele[i][j]; 100 } 101 } 102 return *this; 103} 104 105 106//以下省略
実現したい動作
main.cpp
c++
1#include <bits/stdc++.h> 2#include "Matrix.h" 3using namespace std; 4 5int main(){ 6 Matrix M(3, 3); 7 M.set(1.0, 2.0, 3.0, 8 4.0, 5.0, 6.0, 9 7.0, 8.0, 9.0); 10 M.show() 11 12 Matrix R(1, 3); 13 R.set(1.0, 1.0, 1.0); 14 M.row(0) = R; 15 M.show(); 16 //列の方も同様 17} 18 19/* 20標準出力に次のように出力 211.0 2.0 3.0 224.0 5.0 6.0 237.0 8.0 9.0 24 251.0 1.0 1.0 264.0 5.0 6.0 277.0 8.0 9.0 28 29*/ 30
###試したこと
Matrix&型ではなく、vector<double>&型を返す関数なら、行に関してのみ実装できました。列に関してはこっちもできていません。
c++
1vector<double>& Matrix::row(int n){ 2 return ele[n]; 3}
これは簡単ですね、、、。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/18 15:25 編集
2017/10/18 16:35
2017/10/19 05:47