Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
inner_product.cpp
Go to the documentation of this file.
1
30#include <algorithm> // for copy()
31#include <functional> // for plus, minus
32#include <iostream> // for cout, endl
33#include <iterator> // for ostream_iterator
34#include <list> // for list
35#include <numeric> // for inner_product()
36#include <vector> // for vector
37
38#include <examples.h>
39
40
41int main ()
42{
43 typedef std::list<int, std::allocator<int> > List;
44 typedef std::vector<int, std::allocator<int> > Vector;
45 typedef std::ostream_iterator<int, char, std::char_traits<char> > os_iter;
46
47 // Initialize a list and a vector using arrays of ints.
48 int a1[] = { 6, -3, -2 };
49 int a2[] = { -2, -3, -2 };
50
51 List l (a1, a1 + sizeof a1 / sizeof *a1);
52 Vector v (a2, a2 + sizeof a2 / sizeof *a2);
53
54 // Calculate the inner product of the two sets of values.
55 List::value_type prod = std::inner_product (l.begin (), l.end (),
56 v.begin (), 0);
57
58 // Calculate a wacky inner product using the same values.
59 List::value_type wacky =
60 std::inner_product (l.begin (), l.end (), v.begin (), 0,
61 std::plus<List::value_type>(),
62 std::minus<List::value_type>());
63 // Print the output.
64 std::cout << "For the sets of numbers: { ";
65 std::copy (v.begin (), v.end (), os_iter (std::cout, " "));
66
67 std::cout << "} and { ";
68 std::copy (l.begin (), l.end (), os_iter (std::cout, " "));
69
70 std::cout << "}\nThe inner product is: " << prod
71 << "\nThe wacky result is: " << wacky
72 << std::endl;
73
74 return 0;
75}
#define v
Definition 17.names.cpp:138
#define a2
Definition 17.names.cpp:251
#define a1
Definition 17.names.cpp:225
std::vector< UserClass, std::allocator< UserClass > > Vector
int main()