How to load list of numbers into a vector??

Hi,

I have a file with lists of numbers separated by white space on multiple lines.

e.g

1 2 3 4 5
3 4 5 6 7 4 7 7
1 5 7 7 4 3 4

I need to import each line into a different vector

e.g

vector 1 [1,2,3,4,5]
vector 2 [3,4,5,6,7,4,7,7]
etc

I don't know how many numbers will be on each line as it could be 1 or 100

How would you suggest going about this?

Cheers!



Read each line into a std::string and then parse the string to extract the numbers into a std::vector<int>

Something like this (error handling of badly formed lines elided for brevity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

std::vector<int> parse_line( std::string line )
{
    std::istringstream stm(line) ;
    return { std::istream_iterator<int>(stm), std::istream_iterator<int>() } ;
}

std::vector< std::vector<int> > read_lines( std::istream& stm )
{
    std::vector< std::vector<int> > result ;

    std::string line ;
    while( std::getline( stm, line ) ) result.push_back( parse_line(line) ) ;

    return result ;
}

http://coliru.stacked-crooked.com/a/87fc8840725db385
Cheers for the reply.

I'm only a beginner and have yet to cover any #<sstream> and #<iterator> stuff.

I known that I have 8 lines that need to be stored in 8 vectors. Is there anyway to read just the first line then store it in a named vector. Then the second to another named vector etc???

> yet to cover any #<sstream> and #<iterator> stuff.

If you know how to use file stream, you already know how to use string stream. Sans the <iterator> stuff:

1
2
3
4
5
6
7
8
9
10
11
std::vector<int> parse_line( std::string line )
{
    std::vector<int> result ; 
    
    std::istringstream stm(line) ;
    
    int value ;
    while( stm >> value ) result.push_back(value) ;
    
    return result ;
}

http://coliru.stacked-crooked.com/a/4a807d6487263641


> Is there anyway to read just the first line then store it in a named vector.
> Then the second to another named vector etc???

Without reading the line into a string (eschewing string stream), the code becomes more complex; even without having to handle errors in input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <cctype>
#include <fstream>

bool read_one( std::istream& stm, int& value ) // read the next integer
{
    char c ;
    
    // extract and throw away characters till a non-whitespace or new line is extracted 
    //                                                    or there is an input failure.
    while( stm.get(c) && c != '\n' && std::isspace(c) ) ;
    
    if( c == '\n' ) return false ; // extracted new line; nothing more to read from this line
    
    stm.putback(c) ; // extracted a non-whitespace character, put it back
    return bool( stm >> value ) ; // and try to read an integer
}

std::vector<int> read_line( std::istream& stm ) // read all integers in one line
{
    std::vector<int> result ;

    int value ;
    while( read_one( stm, value ) ) result.push_back(value) ;

    return result ;
}


int main()
{
    const char* const path = "test.txt" ;

    // create a test file
    { std::ofstream(path) << "1 2 3 4 5 \n 3 4 5 6 7 4 7 7 \n 1 5 7 7 4 3 4 \n" ; }

    std::ifstream file(path) ;
    std::vector<int> a = read_line(file) ;
    std::vector<int> b = read_line(file) ;
    std::vector<int> c = read_line(file) ;

    for( const auto& vec : { a, b, c } )
    {
        std::cout << "[ " ;
        for( int value : vec ) std::cout << value << ' ' ;
        std::cout << "]\n" ;
    }
}

http://coliru.stacked-crooked.com/a/b73e2a2b9dde706b
I've played around with the method storing the numbers and instead gone with storing them in columns and then loading them into a parallel vector.

1 1 3 4
2 4 5 6
1 5 6 7

vector[1,2,1]
vector[1,4,5]

Would that make loading easier? I have written something along the lines of;

while(readFile>>w>>d>>h>>x)

vector.pushback(w)
vector2.pushback(d)

etc

however, it only seems to be import the first line.


1
2
3
4
5
6
while(readFile>>w>>d>>h>>x)

vector.pushback(w)
vector2.pushback(d)

etc


I don't know how many numbers will be on each line as it could be 1 or 100


... doesn't this mean you would need to have a variable number of vectors ?

The number of vectors is fixed as it's 1 for each column, there will be a variable number of elements in each vector though.

Cheers.
Sorry probably should have made clearer, that the method of storing the number in the first post has been completely changed. Instead of each 1 vector on 1 line, 1 vector per column.

So the number of vectors is known, but the number of elements in each vector is variable, but there will be the same number of elements in all the vectors.

This problem has been blowing my mind all week, thanks for taking a look!
Topic archived. No new replies allowed.