C++中的变量矩阵名称

Variable Matrix name in C++

本文关键字:变量 C++      更新时间:2023-10-16

我试图在一个循环中读取多个文件,并试图将文件的内容存储在矩阵中,即对于循环中的每个文件,我希望将内容存储在新的矩阵中。但我找不到任何使用c++的方法。所以我在问c++专家,有什么出路吗?

欢迎提出任何建议。谢谢

问题的解决方案可能是将矩阵存储在某个容器中(std::vector、std::list、std::array,具体取决于您的特定需求)。

澄清:如果一个矩阵是

std::vector<std::vector<int>> 

你在找

std::vector<std::vector<std::vector<int>>>

std::list<std::vector<std::vector<int>>> 

等等。

代码示例:(用你用来表示一个矩阵的任何类型来代替矩阵)

std::vector<Matrix> M;
for (...)
{ ... // read new Matrix into Matrix newM
   M.push_back(newM);
}

那么你的矩阵被称为M[0],M[1],。。。,M[n-1],如果你有n个矩阵。

您要查找的是一个映射,std::mapstd::unordered_map。这将允许您将字符串映射到矩阵,例如:

std::map<std::string, Matrix> matrices;
matrices.insert( std::make_pair( fileName, getMatrixFromFile( fileName )  ) );

等等。

如果您已经有一些矩阵实现来存储单个文件的内容,那么您可以std::vector来存储与不同文件对应的矩阵

std::vector< Matrix_t > matrices;   
for(...) // loop over the files
{ 
    Matrix_t matrix;
    ... // read file into the matrix
    matrices.push_back(matrix);
}

在循环结束时,您将得到每个文件包含一个矩阵的向量。