C++ (C+;+;)有没有办法在循环中创建不同的矩阵?

C++ (C+;+;)有没有办法在循环中创建不同的矩阵?,c++,arrays,matrix,dynamic-memory-allocation,C++,Arrays,Matrix,Dynamic Memory Allocation,我正在写一个代码,它读取一个文本文件来创建一个矩阵。然后我将从这个矩阵中获取信息,并将其写入另一个文本文件中 我加入了一个“for”循环,该循环将为它读取的每个文本块创建一个矩阵,如下所示: for (cont = 0; cont < nPares; cont++){ ResultFile >> FlowOri; ResultFile >> FlowDest; ResultFile >&

我正在写一个代码,它读取一个文本文件来创建一个矩阵。然后我将从这个矩阵中获取信息,并将其写入另一个文本文件中

我加入了一个“for”循环,该循环将为它读取的每个文本块创建一个矩阵,如下所示:

    for (cont = 0; cont < nPares; cont++){
        
        ResultFile >> FlowOri;
        ResultFile >> FlowDest;
        ResultFile >> nPaths;
        ResultFile >> largestPath;

        double** iPathMatrix = (double**) new double[nPaths];
        for (i = 0; i < nPaths; i++) {
            iPathMatrix[i] = (double*) new double[largestPath + 2];
        }

        for (i = 0; i < nPaths; i++) {
            for (j = 0; j < largestPath + 2; j++) {
                ResultFile >> iPathMatrix[i][j];
            }
        }

        for (i = 0; i < nPaths; i++) {
            for (j = 0; j < largestPath + 2; j++) {
                cout << iPathMatrix[i][j] << " ";
            }
            cout << "\n";
        }

        free(iPathMatrix);
    }
for(cont=0;cont>FlowOri;
结果文件>>FlowDest;
结果文件>>NPath;
结果文件>>最大路径;
双**iPathMatrix=(双**)新双[nPath];
对于(i=0;i>iPathMatrix[i][j];
}
}
对于(i=0;i
不,您没有(至少没有正确)。每个新[]必须与一个delete[]配对,因此正确的代码是

   for (i = 0; i < nPaths; i++) {
        delete[] iPathMatrix[i];
   }
   delete[] iPathMatrix;

现在,稍后当您想要检索matirx时,只需编写
matrixMap[something]<代码> >某物< /C>是一个变量,它有一个要检索的矩阵的名称。

变量名只对程序员有意义,除非使用调试模式,否则它将从可执行文件中消失,因此C++不允许创建动态命名变量。无论如何,即使使用允许的语言,它也是一个可怕的设计。n

但您始终可以创建任何对象类型的数组(或更好的向量)。因此,如果您希望在循环中存储所有内容并在以后处理整个数据,您可以使用3级向量。借助引用的魔力,代码中几乎不需要做任何更改:

std::vector<std::vector<std::vector<double>>> matrixes(nPares);
for (auto& iPathMatrix : matrixes) {    // iPathMatrix is a reference inside matrixes
    ResultFile >> FlowOri;
    ResultFile >> FlowDest;
    ResultFile >> nPaths;
    ResultFile >> largestPath;

    iPathMatrix = std::vector<std::vector<double>>(nPaths);
    for (i = 0; i < nPaths; i++) {
        iPathMatrix[i] = std::vector<double>(largestPath + 2];
    }

    for (i = 0; i < nPaths; i++) {
        for (j = 0; j < largestPath + 2; j++) {
            ResultFile >> iPathMatrix[i][j];
        }
    }

}
// control:
for (int i = 0; i < nPares; i++) {
    for (int j = 0; j < matrixes[i].size(); j++) {
        for (int k = 0; k < matrixes[i][j].size(); k++) {
            std::cout << matrixes[i][j][k] << " ";
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}    
std::向量矩阵(nPares);
对于(自动&iPathMatrix:Matrix){//iPathMatrix是矩阵内部的引用
结果文件>>FlowOri;
结果文件>>FlowDest;
结果文件>>NPath;
结果文件>>最大路径;
iPathMatrix=std::vector(NPath);
对于(i=0;i>iPathMatrix[i][j];
}
}
}
//控制:
for(int i=0;istd::cout当您删除
new
前面不必要的强制转换时,第一个将出现错误。它应该是
new double*[npath]“也许是以前没有一个地图,但是现在我有一个新的概念。FooRI和FuST都是int变量,我忘了提过,抱歉,我从来没有用过地图,而且我还没有在C++上编程太长时间。,我会边走边把它捡起来,因为我的导师把我放在了这个正在使用它的项目中。听起来它可能会起作用,我会查找一些关于地图的资料并试一试。我会在测试时给出反馈,谢谢。我先测试了另一个答案,因为它看起来更容易实现,而且效果很好。我想我有点误导了你因为我意识到我以后不需要使用'FlowOri'变量或任何其他变量来引用它,所以对此表示抱歉。尽管如此,这是我将研究的另一个工具,因此非常感谢!这非常有效,而且易于实现,非常感谢!
#include <vector>
#include <map>
#include <string>

// lets give a shorter name for the vector type
using MatrixRowType = std::vector<double>;
using MatrixType = std::vector<MatrixRowType>;

// and this is the map type that holds the matrices
using MatrixMapType = std::map<std::string, MatrixType>;

...

MatrixMapType matrixMap;
for (cont = 0; cont < nPares; cont++){
    // read stuff
    ResultFile >> FlowOri;
    ...
    // read the matrix
    MatrixType iPathMatrix(nPaths, MatrixRowType(largestPath + 2));
    ...
    // save the matrix in the map keyed by FlowOri
    matrixMap[FlowOri] = iPathMatrix;
}
std::vector<std::vector<std::vector<double>>> matrixes(nPares);
for (auto& iPathMatrix : matrixes) {    // iPathMatrix is a reference inside matrixes
    ResultFile >> FlowOri;
    ResultFile >> FlowDest;
    ResultFile >> nPaths;
    ResultFile >> largestPath;

    iPathMatrix = std::vector<std::vector<double>>(nPaths);
    for (i = 0; i < nPaths; i++) {
        iPathMatrix[i] = std::vector<double>(largestPath + 2];
    }

    for (i = 0; i < nPaths; i++) {
        for (j = 0; j < largestPath + 2; j++) {
            ResultFile >> iPathMatrix[i][j];
        }
    }

}
// control:
for (int i = 0; i < nPares; i++) {
    for (int j = 0; j < matrixes[i].size(); j++) {
        for (int k = 0; k < matrixes[i][j].size(); k++) {
            std::cout << matrixes[i][j][k] << " ";
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}