0
我得到了這個硬件問題,我真的堅持下去。我需要從文件加載這個矩陣。從文件加載矩陣
class matrix
{
public:
matrix(matrix& other); //copy constructor
~matrix(); //destructor
void LoadMatrix(string filename);
//load data from file into m
bool operator==(matrix& other); //compare two matrix
private:
int** m; //2D dynamic array of integers
int height; //height of m
int width; //width of m
};
從數據文件中的LoadMatrix
功能加載數據,數據文件包含類似的數據:
2 3
1 2 3
3 4 8
這是我到目前爲止有:
class matrix
{
public:
matrix(matrix& other); //copy constructor
~matrix(); //destructor
void LoadMatrix(string filename);
//load data from file into m
bool operator==(matrix& other); //compare two matrix
private:
int** m; //2D dynamic array of integers
int height; //height of m
int width; //width of m
};
matrix::matrix()
{
}
matrix::~matrix()
{
for (int i = 0; i < height; i++)
{
delete m[i];
}
delete m;
}
void matrix::LoadMatrix(string filename)
{
ifstream infile(filename);
if (infile.is_open())
{
string line1 = "", line2 = "";
infile >> height;
infile >> width;
infile.ignore();
m = new int*[height];
for (int i = 0; i < height; i++)
{
m[i] = new int[width];
line2 = "";
getline(infile, line1);
}
}
}
int main()
{
}
任何提示將是巨大的, 謝謝!
閱讀'n',讀取'm',讀取'm'整數的'n'行成一個數組。問題是什麼? –
你爲什麼要這樣設計你的代碼? –