c++11 - Reading multiple column and put them in vector in c++ -


i have file containing values in 15 columns , 1000+ rows.

45285785.00 45285797.00 45285776.00 45285795.00 45285785.00 45285803.00 45285769.00 45285773.00 45285771.00 45285795.00 45285771.00 45285798.00 45285796.00 45285797.00 45285753.00 35497405.00 35497437.00 35497423.00 35497465.00 35497463.00 35497468.00 35497437.00 35497481.00 35497417.00 35497479.00 35497469.00 35497454.00 35497442.00 35497467.00 35497482.00 46598490.00 46598483.00 46598460.00 46598505.00 46598481.00 46598480.00 46598477.00 46598485.00 46598494.00 46598478.00 46598482.00 46598495.00 46598491.00 46598491.00 46598476.00 

i want read file. way i'm doing right taking 15 variables , put them vectors individually.

double col1, col2, ... , col15; vector <double> c1, c2, ..., c15; ifstream input('file'); while(input >> col1 >> col2 >> ... >> col15) {     c1.push_back(col1);     c2.push_back(col2);     ...     c15.push_back(col15); } 

is there better way this? mean without defining 15 variables, reading 15 columns in while loop?

yes, there is.

you can consider container of cols, e.g. vector<double> cols(15) , replace c1, c2 ... c15 variables vector of vectors. sth like:

for(int i=0; i<cols.size(); ++i) {     input >> cols[i];     c[i].push_back(cols[i]); } 

however in case not know when should stop reading. fix it, work either input.eof() or input.good() methods or try catch fstream >> operator's return value did before in code.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -