使用C++向量从文本文件读取矩阵

3

我们有一个文本文件中的矩阵,数字之间用逗号分隔,但是每行末尾没有逗号。

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

我试图使用一个二维数组来实现,但由于矩阵的大小未知,所以并不是很有效。

string array[4][4];
int id;
for (int i = 0; i < 4; i++) { // go through each line
    for (int j = 0; j < 4; j++) {
           getline(filein, numbers, ',');
           array[i][j] = numbers;
           cout << array[i][j] << endl;
    }
}

我想使用2D向量来实现,但是我不知道如何做到。比如在创建一个向量之后

vector<vector<string>> matrix;

在循环中,我是否应该创建一个额外的向量?
2个回答

5

使用向量的向量。以下是每行注释:

std::vector<std::vector<int>> v;       // declare vector of vectors
std::ifstream ifs("myfile.txt");       // open the file
std::string tempstr;                   // declare a temporary string
int tempint;                           // declare a temporary integer
char delimiter;                        // declare a temporary delimiter
while (std::getline(ifs, tempstr)) {   // read line by line from a file into a string
    std::istringstream iss(tempstr);   // initialize the stringstream with that string
    std::vector<int> tempv;            // declare a temporary vector for the row
    while (iss >> tempint) {           // extract the numbers from a stringstream
        tempv.push_back(tempint);      // push it onto our temporary vector
        iss >> delimiter;              // read the , delimiter
    }
    v.push_back(tempv);                // push the vector onto vector of vectors
}

完整的源代码如下:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>

int main() {
    std::vector<std::vector<int>> v;
    std::ifstream ifs("myfile.txt");
    std::string tempstr;
    int tempint;
    char delimiter;

    while (std::getline(ifs, tempstr)) {
        std::istringstream iss(tempstr);
        std::vector<int> tempv;
        while (iss >> tempint) {
            tempv.push_back(tempint);
            iss >> delimiter;
        }
        v.push_back(tempv);
    }

    for (auto row : v) {
        for (auto el : row) {
            std::cout << el << ' ';
        }
        std::cout << "\n";
    }
}

嗨,Ron。@Ron 我们需要为 delimiter 分配一些值吗?如果文件中的实际分隔符包含多个字符怎么办? - Scott Yang

0

使用双重std::vector,逐行读取文件并解析逗号,就完成了。每次读取一行时,将向量的大小增加1。您可以使用std::vector::resize()来实现。

例如,使用您的文件名为“matrix.txt”:

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

int main(void) {
    std::vector<std::vector<int>> matrix;
    std::ifstream infile("matrix.txt");
    int a, b, c, d;
    char comma;
    while (infile >> a >> comma >> b >> comma >> c >> comma >> d)
    {
        //std::cout << a << " " << b << " " << c << " " << d << std::endl;
        matrix.resize(matrix.size() + 1);
        matrix[matrix.size() - 1].push_back(a);
        matrix[matrix.size() - 1].push_back(b);
        matrix[matrix.size() - 1].push_back(c);
        matrix[matrix.size() - 1].push_back(d);
    }

    for(auto row: matrix) {
        for(auto v: row) {
            std::cout << v << " ";
        }
        std::cout << "\n";
    }
    return 0;
}

输出:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
1 2 3 4 
7 8 2 1 
3 4 5 6 
7 2 1 3 

你知道你的文件中每一行都有4个数字和3个逗号 @rektandlove ;) - gsamaras
我尝试使用while循环做了些事情,但它不起作用(修改了其他人发布的代码,但似乎已被删除)。 - rektandlove
尝试使用以下代码(std::getline(filein, numbers,',')) - rektandlove

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接