簡體   English   中英

C++:將具有任意行數和列數的文件讀入向量的向量中

[英]C++: Read a file with arbitrary number of rows and columns into a vector of vectors

1)我的輸入:一個 csv(或 txt)文件,包含任意(和無限)列和行的整數(索引),例如

0, 1
2, 3, 4

2)期望output:將integer文件存入數據結構。 我懷疑能夠容納靈活大小的最佳此類數據結構是向量的向量。

3)到目前為止我做了什么,還有什么要做:我已經完成了第一階段和更瑣碎的問題,我將一個格式化的 nx2 csv 文件讀入兩個 arrays。這個程序如下所示。

那么問題是我如何將其擴展到 nxm(而不是 nx2)文件大小並存儲到向量的向量中而不是兩個 arrays?

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include<vector>
using namespace std;

int main(){
    //define variables
    string ind, p; //variables from file are here (two columns)
    vector<int>index;
    vector<float>prob;

    //number of lines
    int i=0;

    ifstream input;
    input.open("Params05.csv"); // Params is the nx2 input file

    while(input.good()){
        //ignore first line
        string line;
        getline(input, line);

        // Two getlines for two columns (sets)
        getline(input, ind, ',');
        index.push_back(stoi(ind));
        getline(input, p, '\n'); //newline
        prob.push_back(stof(p));

        i += 1; //increment total number of laws
        
    }
    input.close(); //close file

    cout<< "Number of entries: "<< i-1 << endl;

    for (int j = 0; j < i; j++){
        cout<< index[j] << "\t" << prob[j] << endl;
    }
}

提前感謝您的任何想法、提示和貢獻。 喜歡這個論壇。

簡而言之,您只需要一個額外的循環和一個字符串流來從每一行中讀取整數

#include <vector>
#include <string>
#include <sstream>
using namespace std;

vector<vector<int>> vec2d;
string line;
getline(file,line); // ignore the first line
while (getline(file, line)) {
    vec2d.push_back(vector<int>()); // add row
    istringstream buffer(line);
    string num;
    while (getline(buffer, num, ',')) {
        vec2d.back().push_back(stoi(num)); // add number to last row
    }
}

希望可以,我還沒有測試任何東西

你需要另一個循環。 您應該為浮動添加測試,但我認為您可以調整此代碼。

輸入文件:

0, 1
2, 3, 4
3, 4, 8, 9

代碼:

using namespace std;

int main(){
    //define variables
    string ind, p; //variables from file are here (two columns)
    vector<vector<int>> index;
    vector<float>prob;

    //number of lines
    int i=0;

    ifstream input;
    input.open("file.txt"); // Params is the nx2 input file

    while(input.good()){
        //ignore first line
        string line;
        getline(input, line);
        if (line == "")
            break;
        cout << "Readed: " << line << endl;
        std::istringstream sinput (line);
        index.push_back (vector<int>());
        while (sinput.good()) {
            getline(sinput, ind, ',');
            index[i].push_back(stoi(ind));
        }
        i += 1; //increment total number of laws
    }
    input.close(); //close file

    cout<< "Number of entries: "<< i << endl;

    for (int j = 0; j < i; j++){
        cout << j << ": ";
        for (auto v : index[j]) {
            cout << v << ", ";;
        }
        cout << endl;
    }
}

Output:

Readed: 2, 3, 4
Readed: 3, 4, 8, 9
Number of entries: 3
0: 0, 1, 
1: 2, 3, 4, 
2: 3, 4, 8, 9, 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM