-1

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.

3 Answers3

2

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>

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
2

You have defined a vector of vectors like this:

vector<vector<string> v

then if you analyze the following instruction:

v[c][j].push_back(line);

then you are calling a push_back(line) method into a string.

v is a vector holding vectors of strings

v[i] is a vector of strings at index i

v[i][j] is a string at index j of the vector at index i

that is the reason of the error

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

You need to use v[c][j] = line; instead of v[c][j].push_back(line);. v[c][j] returns a mutable ref of type string. However string does not have push_back() method.

And as v[c][j] is a mutable ref it can be assigned to a new value.

fadedreamz
  • 1,156
  • 1
  • 10
  • 19