[英]I don't know how to get a const value for my matrix that is read in from a file
I'm so confused, I wouldn't be surprised if you were confused by my question. 我很困惑,如果你对我的问题感到困惑,我不会感到惊讶。 What I am trying to do is create a program that reads in vector/matrix dimensions/contents from a file, creates arrays, and then performs operations.
我要做的是创建一个程序,从文件中读取向量/矩阵维度/内容,创建数组,然后执行操作。
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();
I am obviously getting the error that states that I have to have a constant value. 我显然得到的错误表明我必须有一个恒定的值。 However, since I'm reading in from a file, I'm not sure how to get a constant before I've read the file.
但是,因为我正在读取文件,所以我不确定在读取文件之前如何获得常量。
The file contains lines such as 34#123456789123 that translates to a 3 x 4 matrix with contents 1234...3. 该文件包含34#123456789123等行,转换为内容为1234 ... 3的3 x 4矩阵。 Most of the code up there is how I've read the file in, separated the strings into size and contents, and converted them to ints.
大多数代码都是我如何读取文件,将字符串分成大小和内容,并将它们转换为整数。
The function's parameters are (for example, matrix * matrix): 函数的参数是(例如,矩阵*矩阵):
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;
}
EDIT: I should mention that although I can clearly see that that matrix is 3x4, the next matrix is 4x3. 编辑:我应该提一下,虽然我可以清楚地看到该矩阵是3x4,但下一个矩阵是4x3。 When I run the program, I shouldn't have any input, the program should be able to do it just from reading the file.
当我运行程序时,我不应该有任何输入,程序应该只能通过读取文件来完成。
If you need to make an array and you will not know the size of the array until run-time you need to use pointers. 如果你需要创建一个数组,并且在运行时你不知道数组的大小,你需要使用指针。 You can do that like:
你可以这样做:
int rows;
std::cout << "Rows: ";
std::cin >> rows;
int * values = new int[rows];
// when done call delete [];
delete [] values;
If you need to do 2 dimensions then you can use: 如果您需要做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.