已知矩阵为2行3列,重载流插入运算符“<<”和流提取运算符">>",使之能完成矩阵的输入和输出,完善下列代码 #include <iostream> using namespace std; //请在begin和end间完成Matrix类的编写,重载>>及<<运算符,建议重载为友元函数,矩阵元素可以为double型 /begin*/ /end/ int main() { Matrix m1; cin>>m1; cout<<"output matrix"<<endl; cout<<m1; }
时间: 2023-07-15 12:12:24 浏览: 142
基于矩阵分解的高光谱数据特征提取
以下是完整的代码:
#include <iostream>
using namespace std;
class Matrix {
private:
double data[2][3]; // 矩阵元素
public:
friend istream& operator>>(istream& ins, Matrix& matrix); // 流提取运算符
friend ostream& operator<<(ostream& outs, const Matrix& matrix); // 流插入运算符
};
istream& operator>>(istream& ins, Matrix& matrix) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
ins >> matrix.data[i][j];
}
}
return ins;
}
ostream& operator<<(ostream& outs, const Matrix& matrix) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
outs << matrix.data[i][j] << " ";
}
outs << endl;
}
return outs;
}
int main() {
Matrix m1;
cin >> m1;
cout << "output matrix" << endl;
cout << m1;
return 0;
}
阅读全文