Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
adj_diff.cpp
Go to the documentation of this file.
1
29#include <numeric> // for adjacent_difference
30#include <vector> // for vector
31#include <iostream> // for cout
32#include <iterator> // for ostream_iterator
33#include <functional> // for multiplies
34
35#include <examples.h>
36
37
38int main ()
39{
40 // Typedefs for convenience.
41 typedef std::vector<long, std::allocator<long> > Vector;
42
43 typedef std::ostream_iterator <Vector::value_type,
44 char,
45 std::char_traits<char> > os_iter;
46
47 // Initialize a Vector of ints from an array.
48 const Vector::value_type arr [] = {
49 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
50 };
51
52 Vector v (arr + 0, arr + sizeof arr / sizeof *arr);
53
54 // Two vectors initialized to all zeros for storing results.
55 Vector diffs (v.size ()),
56 prods (v.size ());
57
58 // Calculate difference(s) using default operator (minus).
59 std::adjacent_difference (v.begin (), v.end (), diffs.begin ());
60
61 // Calculate difference (s) using the times operator.
62 std::adjacent_difference (v.begin (), v.end (), prods.begin (),
63 std::multiplies<Vector::value_type> ());
64
65 // Create an ostream_iterator object (for convenience).
66 os_iter osit (std::cout, " ");
67
68 // Output the results.
69 std::cout << "For the vector: " << std::endl << " ";
70 std::copy (v.begin (), v.end (), osit);
71
72 std::cout << std::endl << std::endl;
73
74 std::cout << "The differences between adjacent elements are: "
75 << std::endl << " ";
76
77 std::copy (diffs.begin (), diffs.end (), osit);
78 std::cout << std::endl << std::endl;
79
80 std::cout << "The products of adjacent elements are: "
81 << std::endl << " ";
82
83 std::copy (prods.begin (), prods.end (), osit);
84 std::cout << std::endl;
85
86 return 0;
87}
#define v
Definition 17.names.cpp:138
std::vector< UserClass, std::allocator< UserClass > > Vector
int main()
Definition adj_diff.cpp:38