Re: std::istreambuf_iterator
On Aug 9, 1:57 pm, ahmadcorp <manz...@gmail.com> wrote:
This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?
It's a very simple problem, once you've seen it. On the other
hand, I don't know of anyone who hasn't hit it once or twice.
I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf_iterator as follows (where inputFile is an ifstream):
std::vector<std::string>
data( std::istreambuf_iterator<std::string>(inputFile) ,
std::istreambuf_iterator<std::string>())
Attention: here, you are not defining a variable, but declaring
a function. (Isn't C++ declaration syntax horrible.) You need
to do something so that at least one of the "arguments" can't be
interpreted as a parameter declaration; an extra pair of
parentheses will do the trick:
std::vector< std::string >
data(
(std::istreambuf_iterator< std::string >( inputFile )),
(std::istreambuf_iterator< std::string >()) ) ;
(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34