简体   繁体   English

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

[英]Read matrix from text file using vectors in C++

we have a matrix in text file like this with commas between numbers, but there is no comma at the end of each line. 我们在这样的文本文件中有一个矩阵,数字之间有逗号,但每行末尾没有逗号。

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

I was trying to do this with a 2D array like this but it wasn't really working out because also size of matrix is unknown. 我试图用这样的2D数组做这个,但它并没有真正解决,因为矩阵的大小也是未知的。

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;
    }
}

I want to do that with using 2D vectors but I have no idea how to do that. 我想用2D矢量做到这一点,但我不知道该怎么做。 Like after creating a vector with 就像创建一个矢量后

vector<vector<string>> matrix;

Should I create one more vector inside the loops? 我应该在循环中再创建一个向量吗?

Use vector of vectors. 使用矢量矢量。 Here is the commentary for each line: 以下是每行的评论:

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
}

The full source code is: 完整的源代码是:

#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";
    }
}

Use a double std::vector , read the file line by line, and parse the commas as well, and you are done. 使用double std::vector ,逐行读取文件,并解析逗号,然后就完成了。 Every time you read a line, increase the vector's size by 1. You can do that with std::vector::resize() . 每次读取一行时,将向量的大小增加1.您可以使用std::vector::resize()

Example, using your file as "matrix.txt": 例如,将您的文件用作“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;
}

Output: 输出:

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 

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

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