【问题标题】:Read matrix from text file using vectors in C++使用 C++ 中的向量从文本文件中读取矩阵
【发布时间】:2018-03-10 18:10:41
【问题描述】:

我们在文本文件中有一个这样的矩阵,数字之间有逗号,但每行末尾没有逗号。

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;

我应该在循环内再创建一个向量吗?

【问题讨论】:

    标签: c++ loops matrix vector io


    【解决方案1】:

    使用双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 ;)
    • 我已经尝试过使用 while 循环但它不起作用(修改了其他家伙发布的代码但似乎已删除)
    • 试图使用这个 (std::getline(filein, numbers,',')
    【解决方案2】:

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

    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 我们需要给 delimiter 赋值吗?如果文件中的实际分隔符包含多个字符怎么办?
    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多