c++ - Read file from txt using 2d vectors -


is possible use operator [] in case of 2d vectors? example got following code:

vector<vector<string>> data;  ifstream myreadfile; myreadfile.open("stoixeia_astheni.txt");   while (!myreadfile.eof()) {      for(int i=0; i<1; i++){         (int j=0; j<4; j++){             myreadfile  >> data[i][j];         }     }  } 

i got message out of range. have file 5 lines , 4 columns.

your vector data empty, size() 0. have resize first or add new elements using push_back():

while (!myreadfile.eof()) {         for(int = 0; < 1; i++){         vector<string> tmpvec;         string tmpstring          (int j = 0; j < 4; j++){             myreadfile  >> tmpstring;             tmpvec.push_back(tmpstring);         }         data.push_bac(tmpvec);     } } 

you set size right @ declaration of data:

vector<vector<string>> data(5,vector<string>(4));  ifstream myreadfile; myreadfile.open("stoixeia_astheni.txt");  while (!myreadfile.eof()) {      for(int i=0; < 5; i++){         (int j=0; j<4; j++){             myreadfile  >> data[i][j];         }     }  } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -