繁体   English   中英

将带有整数的矩阵从文本文件存储到C ++中的数组中

[英]Store Matrix with integer from a text file into an array in c++

我有一个名为matrices.txt的文件,其中包含两个矩阵,它们分别为3 x3。它们是:

1 2 3
4 5 6
7 8 9
1 2 3 
4 5 6 
7 8 9

我正在尝试从此文件读取数据,并将其存储到数组proto_matrix中,以便稍后将其拆分为两个矩阵。

我遇到的问题是我无法将数字存储在数组中。

我的代码是

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    int i = 0, j, k = 0, n, dimension;
    cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
    cin >> n;
    dimension = n * n;
    int proto_matrix[2 * dimension];

    // make array of two matrices combined, this will be split into two matrices
    ifstream matrix_file("matrices.txt");

    while(matrix_file)
    {
        matrix_file >> proto_matrix[i];
    }
    matrix_file.close();
}

我尝试调试代码,似乎没有整数存储在数组中,只是随机数。

这个:

int proto_matrix[2 * dimension];

是可变长度数组(VLA),它不是标准C ++,但某些编译器扩展支持。 如果使用g++ -pedantic main.cpp编译,则肯定会出现错误。

这意味着,如果必须使用数组,则需要动态分配内存。

您确实不知道,C ++提供了std::vector ,它可以相对轻松地实现您想要的功能。 我相对来说是因为您在一个文件中有两个矩阵,这使得解析该文件有些棘手。 通常,我们将一个矩阵放在一个文件中,将另一个矩阵放在另一个文件中。

  • 2D数组可以表示为2D向量。
  • 您需要两个矩阵,因此需要两个2D向量。

因此,我将使用大小为2(固定大小!)的数组,其中每个元素都将是2D向量。

此外,我将跟踪我现在正在从文件中读取哪个矩阵(第一个或第二个),以及当前读取了多少行,以便填充正确的矩阵单元。

index将等于0或1,以对数组进行索引。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
    int i = 0, j, k = 0, n, dimension;
    cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
    cin >> n;
    dimension = n * n;
    vector< vector<int> > v[2];
    v[0].resize(n);
    v[1].resize(n);
    // make array of two matrices combined, this will be split into two matrices
    ifstream matrix_file("matrices.txt");
    string line;
    int rows_read = 0, cols_read = 0, index = 0;
    while (std::getline(matrix_file, line))
    {
        std::istringstream iss(line);
        int a, b, c;
        if (!(iss >> a >> b >> c)) { break; } // error
        // process tuple (a, b, c)
        if(index == 0 && rows_read >= n)
        {
            rows_read = 0;
            index = 1;
        }
        //cout<<"rows = " << rows_read << " " << index<<endl;

        v[index][rows_read].push_back(a);
        v[index][rows_read].push_back(b);
        v[index][rows_read++].push_back(c);
    }
    matrix_file.close();

    for(int i = 0; i < 2; ++i)
    {
        cout << "Printing matrix " << i << endl;
        for(auto& matrix: v[i])
        {
            for(auto& number: matrix)
                cout << number << " ";
            cout << endl;
        }
    }
    return 0;
}

输出:

Printing matrix 0
1 2 3 
4 5 6 
7 8 9 
Printing matrix 1
1 2 3 
4 5 6 
7 8 9 

PS:与您的问题无关,但是main()在C和C ++中应该返回什么? 一个int

基于我的回答 ,我将在此处发布一个更简洁的示例,该示例将实现您的代码尝试实现的目标:读取一维数据结构中的所有数字(稍后将以某种方式将其拆分为两个矩阵)。

因此,对于这种情况,代码可以归结为:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
    int i = 0, j, k = 0, n, dimension;
    cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
    cin >> n;
    dimension = n * n;
    // make array of two matrices combined, this will be split into two matrices
    vector<int> proto_matrix;
    ifstream matrix_file("matrices.txt");
    string line;
    while (std::getline(matrix_file, line))
    {
        std::istringstream iss(line);
        int a, b, c;
        if (!(iss >> a >> b >> c)) { break; } // error
        proto_matrix.push_back(a);
        proto_matrix.push_back(b);
        proto_matrix.push_back(c);
    }
    matrix_file.close();

    for(auto& number: proto_matrix)
        cout << number << "\n";
    return 0;
}

您无需递增i ,因此始终写入第一个元素。 它应该是:

matrix_file >> proto_matrix[i++];

您可以将所有值读入一个平面容器中(例如std::vector ),并在以后进行重新排列。 以下代码可能会给您一个想法:

#include <vector>
#include <fstream>
#include <iostream>

int main() {
  std::vector<int> values;

  std::ifstream fp("matrices.txt");

  for (int value; fp >> value; ) {
    values.emplace_back(value);
  }

  std::cout << "I read " << values.size() << " values:\n";

  for (auto && value : values) std::cout << value << " ";

  std::cout << "\n";
}

结果:

$ clang++ matrixread.cpp -std=c++17
$ ./a.out 
I read 18 values:
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM