Search code examples
c++filevectorifstreampush-back

Is there a more efficient way for storing vector<vector<string>> ? C++


The real file I read is about 15.000 kBs. Thus I am not sure whether this is the best way of storing such a data structure.

Here is the code.

string line;
ifstream File;
vector<vector<string>> v;
string filename = "meddra.txt";
File.open(filename);

if (File.is_open()) {
    while (getline(File, line)) {
        stringstream ss(line);
        vector<string> row;
        while (getline(ss, line, ',')) {
            row.push_back(line);
        }
        v.push_back(row);
    }
}

And here is sample text file:

CID100000085,CID000010917,C0000729,Abdominal cramps CID100000085,CID000010917,C0000737,Abdominal pain CID100000085,CID000010917,C0002418,Amblyopia CID100000085,CID000010917,C0002871,Anaemia CID100000085,CID000010917,C0003123,Anorexia

Thank you for contribution.


Solution

  • You are modifying an empty vector

    vector<vector<string>> v;
    v[c][j].push_back(line);
    

    Instead you should do v.push_back with a vector<string>