Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
stream_iterator.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for copy
30#include <iostream> // for cin, cout, endl
31#include <iterator> // for stream_iterators, inserter
32#include <vector> // for vector
33#include <numeric> // for accumulate
34
35#include <examples.h>
36
37int main ()
38{
39 // Typedefs for convenience.
40 typedef std::vector<int, std::allocator<int> > Vector;
41
42 typedef std::istream_iterator<Vector::value_type,
43 char, std::char_traits<char>,
44 std::ptrdiff_t> is_iter;
45
46 typedef std::ostream_iterator<Vector::value_type,
47 char, std::char_traits<char> > os_iter;
48
49 Vector v;
50
51 Vector::value_type sum = 0;
52
53 // Collect values from cin until end of file
54 // Note use of default constructor to get ending iterator
55 std::cout << "Enter a sequence of integers (eof to quit): " ;
56 std::copy (is_iter (std::cin), is_iter (), std::inserter (v, v.begin ()));
57
58 // Stream the whole vector and the sum to cout.
59 std::copy (v.begin (), v.end () -1, os_iter (std::cout, " + "));
60
61 if (v.size ())
62 std::cout << v.back () << " = " <<
63 std::accumulate (v.begin (), v.end (), sum) << std::endl;
64
65 return 0;
66}
#define v
Definition 17.names.cpp:138
std::vector< UserClass, std::allocator< UserClass > > Vector
int main()