![](/img/trans.png)
[英]Want c++ to get these variables into an array from a text file.But I don't know how to specify locations in a text file
[英]I don't know how to get a const value for my matrix that is read in from a file
我很困惑,如果你对我的问题感到困惑,我不会感到惊讶。 我要做的是创建一个程序,从文件中读取向量/矩阵维度/内容,创建数组,然后执行操作。
ifstream in("input.txt");
stringstream buffer;
buffer << in.rdbuf();
string test = buffer.str();
string line;
ifstream file("input.txt");
string contents;
int* conv;
while (getline(file, line)){
*conv = atoi(line.c_str());
//cout << conv << endl;
size_t pos = line.find("#");
contents = line.substr(pos + 1);
//cout << contents << endl;
}
int row = conv[4];
int column = conv[5];
int aMatrix[row];
file.close();
cin.get();
我显然得到的错误表明我必须有一个恒定的值。 但是,因为我正在读取文件,所以我不确定在读取文件之前如何获得常量。
该文件包含34#123456789123等行,转换为内容为1234 ... 3的3 x 4矩阵。 大多数代码都是我如何读取文件,将字符串分成大小和内容,并将它们转换为整数。
函数的参数是(例如,矩阵*矩阵):
int** mxm(int **A, int **B, int rowsA, int colsA, int rowsB, int colsB){
int** value = NULL;
for (int i = 0; i < rowsA; i++){
for (int j = 0; i < colsA; i++){
for (int k = 0; k < colsB; k++){
**value += A[i][j] * B[i][k];
}
}
}return value;
}
编辑:我应该提一下,虽然我可以清楚地看到该矩阵是3x4,但下一个矩阵是4x3。 当我运行程序时,我不应该有任何输入,程序应该只能通过读取文件来完成。
如果你需要创建一个数组,并且在运行时你不知道数组的大小,你需要使用指针。 你可以这样做:
int rows;
std::cout << "Rows: ";
std::cin >> rows;
int * values = new int[rows];
// when done call delete [];
delete [] values;
如果您需要做2个维度,那么您可以使用:
int rows, cols;
std::cout << "Rows: ";
std::cin >> rows;
std::cout << "Cols: ";
std::cin >> cols;
int ** values = new int[rows];
for (int i = 0; i < rows; i++)
values[i] = new int[cols];
// when done call delete [] for both dimensions;
for (int i = 0; i < rows; i++)
delete [] values[i];
delete [] values;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.