Exercise 3.4

Write a program to read a sequence of integer numbers from standard input using an istream_iterator. Write the odd numbers into one file using an ostream_iterator. Each value should be separated by a space. Write the even numbers into a second file, also using an ostream_iterator. Each of these values should be placed on a separate line.

To read a sequence of integers from standard input, we define two istream_iterators: one bound to cin, and the second representing end-of-file.

istream_iterator<int> in( cin ), eos; 

Next, we define a vector to hold the elements read:

vector< int > input; 

To perform the reading, we use the copy() generic algorithm:

 #include <iterator> #include <vector> #include <iostream> #include <algorithm> ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.