Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read line of numbers using C++

What's the standard way of reading a "line of numbers" and store those numbers inside a vector.

file.in
12 
12 9 8 17 101 2 

Should I read the file line by line, split the line with multiple numbers, and store the tokens in the array ?

What should I use for that ?

like image 682
Andrei Ciobanu Avatar asked Feb 15 '11 15:02

Andrei Ciobanu


1 Answers

#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>

std::vector<int> data;
std::ifstream file("numbers.txt");
std::copy(std::istream_iterator<int>(file), std::istream_iterator<int>(), std::back_inserter(data));
like image 176
Nawaz Avatar answered Oct 06 '22 01:10

Nawaz