Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
accumulate.cpp
Go to the documentation of this file.
1
29#include <numeric> // for accumulate
30#include <vector> // for vector
31#include <functional> // for multiplies
32#include <iostream> // for cout
33
34#include <examples.h>
35
36int main ()
37{
38 // Typedef for convenience.
39 typedef std::vector<int, std::allocator<int> > Vector;
40
41 // Initialize a vector using an array of integers.
42 const Vector::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
43 Vector v1 (arr, arr + sizeof arr / sizeof *arr);
44
45 // Accumulate sums and products.
46 Vector::value_type sum =
47 std::accumulate (v1.begin (), v1.end (), 0);
48
49 Vector::value_type prod =
50 std::accumulate (v1.begin (), v1.end (), 1,
51 std::multiplies<Vector::value_type>());
52
53 // Output the results.
54 std::cout << "For the series: ";
55 for (Vector::iterator i = v1.begin (); i != v1.end (); ++i)
56 std::cout << *i << " ";
57
58 std::cout << "where N = " << v1.size ()
59 << "\nThe sum = (N * N + N) / 2 = " << sum
60 << "\nThe product = N! = " << prod << std::endl;
61
62 return 0;
63}
#define i
Definition 17.names.cpp:126
#define v1
Definition 17.names.cpp:245
std::vector< UserClass, std::allocator< UserClass > > Vector
int main()