简体   繁体   English

我不知道如何获取从文件中读入的矩阵的const值

[英]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.

相关问题 希望 c++ 将这些变量从文本文件中放入数组中。但我不知道如何在文本文件中指定位置 - 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 如果我不希望有人知道或更改硬编码值,宏是否比静态const更安全? - Is macro more secure than static const if I don't want someone to know or change the hardcode value? 我正在尝试从文本文件中读取并将它们分类为结构的不同成员,但我不知道哪里出了问题 - I'm trying to read from a text file and categorize them into different members of structure but i don't know where it went wrong 我的 C++ 应用程序在输入一个值(std::cin >> 值)后关闭,我不知道为什么 - My C++ application closes after entering a value (std::cin >> value) and I don't know why 我不知道它如何工作表[+1]? - I don't know how it work table[+1]? 如何从const引用中获取键的值? - How do I get the value for a key from a const ref? 我不知道如何修复我的代码,它不能正常工作 - I don't know how to fix my code , it doesn't work properly 我无法让程序正确读取输入文件中的值(2D数组) - I can't get my program to read the values from my input file correctly (2D array) 我无法让我的程序在函数中读取我的文件 - I can't get my program to read my file in a function 我想通过Arduino读取ML-NTC2温度传感器值。 但我得到错误:从'char*'到'const uint8_t*'的无效转换 - I want to read the ML-NTC2 temperature sensor value through Arduino. But I get error: invalid conversion from'char*' to'const uint8_t*'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM