Re: C++ Primer ex 9.14
On Sep 16, 1:02 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
[...]
It runs fine. Is there any way I can replace what while loop with
std::copy ?
Might be, you would have to use std::istream_iterators and a back insert
iterator to add the elements to the vector, something like (untested):
std::copy(
std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>() // The same as end() for a stream
std::back_insert_iterator<std::vector>(vec)
);
Even simpler would be to use the istream_iterators to initialize
the container. Something like:
std::vector< std::string > vec(
(std::istream_iterator< std::string >( std::cin )),
(std::istream_iterator< std::string >()) ) ;
(Note that the outermost parentheses around the arguments are
necessary to prevent the compiler from interpreting the
statement as a function declaration.)
--
James Kanze (GABI Software) email:james.kanze@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