Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
partial_sum.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for copy
30#include <functional> // for multiplies
31#include <iostream> // for cout, endl
32#include <iterator> // for ostream_iterator
33#include <numeric> // for partial_sum
34#include <vector> // for vector
35
36#include <examples.h>
37
38
39int main ()
40{
41 typedef std::vector<int, std::allocator<int> > Vector;
42 typedef std::ostream_iterator<int, char, std::char_traits<char> > Iter;
43
44 // Initialize a vector using an array of integers.
45 const Vector::value_type a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
46 Vector v (a + 0, a + sizeof a / sizeof *a);
47
48 // Create an empty vectors to store results.
49 Vector sums (Vector::size_type (10));
50 Vector prods (Vector::size_type (10));
51
52 // Compute partial_sums and partial_products.
53 std::partial_sum (v.begin (), v.end (), sums.begin ());
54 std::partial_sum (v.begin (), v.end (), prods.begin (),
55 std::multiplies<Vector::value_type>());
56
57 // Output the results.
58 std::cout << "For the series: \n ";
59 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
60
61 std::cout << "\n\nThe partial sums: \n ";
62 std::copy (sums.begin (), sums.end (), Iter (std::cout, " "));
63 std::cout << " should each equal (N*N + N)/2\n\n";
64
65 std::cout << "The partial products: \n ";
66 std::copy (prods.begin (), prods.end (), Iter (std::cout, " "));
67 std::cout << " should each equal N!" << std::endl;
68
69 return 0;
70}
#define v
Definition 17.names.cpp:138
#define a
Definition 17.names.cpp:109
std::vector< UserClass, std::allocator< UserClass > > Vector
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
int main()