lundi 7 janvier 2019

How to read effectively data (the same type) from file line after line to several vectors (vector of vectros)?

I have a file with more than 100 pcs. of elements (the same type of data e.g ints). Every elements is in other line. Structure of file:
int
int
...
int

I have to read this data to 2D array (vector of 5 vectors):
- first line to first vector
- second line to second vector
....
- fifth line to fifth vector
- sixth line to first vector...

and from the beginning until the end of the file.

std::vector<std::vector<int>> my_v;
std::ifstream in( "data.txt" );
std::string record;

while ( std::getline( in, record ) )
{
    std::istringstream is( record );
    std::vector<int> row( ( std::istream_iterator<int>( is ) ),
                             std::istream_iterator<int>() );
    my_v.push_back( row );
}

for ( const auto &row : my_v )
{
    for ( double x : row ) std::cout << x << ' ';
    std::cout << std::endl;
}        

Now i'm reading data to one vector. How to fix it ?

Aucun commentaire:

Enregistrer un commentaire