Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg6.cpp
Go to the documentation of this file.
1
29#include <vector>
30#include <list>
31#include <functional>
32#include <algorithm>
33#include <numeric>
34#include <iostream>
35#include <iterator>
36#include <functional>
37
38#include <examples.h>
39
40typedef std::ostream_iterator<int, char, std::char_traits<char> > ostrm_iter_type;
41
42
43int square (int n) {
44 return n * n;
45}
46
47
48class iotaGen {
49public:
50 iotaGen (int iv) : current (iv)
51 { }
53 return current++;
54 }
55private:
56 int current;
57};
58
59
60// Illustrate the use of the transform algorithm.
62
63 // Generate a std::list of values from 1 to 6.
64 std::list<int, std::allocator<int> > aList;
65
66 std::generate_n (std::inserter (aList, aList.begin ()), 6, iotaGen (1));
67 std::cout << "Original list: ";
68 std::copy (aList.begin (), aList.end (), ostrm_iter_type (std::cout, " "));
69
70 std::cout << std::endl;
71
72 // Transform elements by squaring, copy into std::vector.
73 std::vector<int, std::allocator<int> > aVec (6);
74
75 std::transform (aList.begin (), aList.end (), aVec.begin (), square);
76 std::cout << "After squaring: ";
77 std::copy (aVec.begin (), aVec.end (), ostrm_iter_type (std::cout, " "));
78
79 std::cout << std::endl;
80
81 // Transform std::vector again, in place, yielding 4th powers.
82 std::transform (aVec.begin (), aVec.end (), aVec.begin (), square);
83 std::cout << "After squaring again: ";
84 std::copy (aVec.begin (), aVec.end (),ostrm_iter_type (std::cout, " "));
85
86 std::cout << std::endl;
87
88 // Transform in parallel, yielding cubes.
89 std::vector<int, std::allocator<int> > cubes (6);
90
91 std::transform (aVec.begin (), aVec.end (), aList.begin (), cubes.begin (),
92 std::divides<int> ());
93 std::cout << "After division: ";
94 std::copy (cubes.begin (), cubes.end (), ostrm_iter_type (std::cout, " "));
95
96 std::cout << std::endl;
97}
98
99
100// Illustrate the use of the partial sum algorithm.
102
103 // Generate values 1 to 5.
104 std::vector<int, std::allocator<int> > aVec (5);
105 std::generate (aVec.begin (), aVec.end (), iotaGen (1));
106
107 // Output partial sums.
108 std::cout << "Partial sums examples" << std::endl;
109 std::cout << "Partial sums : ";
110 std::partial_sum (aVec.begin (), aVec.end (),
111 ostrm_iter_type (std::cout, " "));
112
113 std::cout << std::endl;
114
115 // Output partial products.
116 std::cout << "Partial products: ";
117 std::partial_sum (aVec.begin (), aVec.end (),
118 ostrm_iter_type (std::cout, " "), std::multiplies<int> () );
119
120 std::cout << std::endl;
121}
122
123
124// Illustrate the use of the adjacent difference algorithm.
126
127 // Generate values 1 to 5.
128 std::vector<int, std::allocator<int> > aVec (5);
129 std::generate (aVec.begin (), aVec.end (), iotaGen (1));
130
131 // Output partial sums.
132 std::cout << "Adjacent Differences examples" << std::endl;
133 std::cout << "Adjacent Differences : ";
134 std::adjacent_difference
135 (aVec.begin (), aVec.end (), ostrm_iter_type (std::cout, " "));
136 std::cout << std::endl;
137
138 // Output partial products.
139 std::cout << "Adjacent sums: ";
140 std::adjacent_difference (aVec.begin (), aVec.end (),
141 ostrm_iter_type (std::cout, " "),
142 std::plus<int> ());
143
144 std::cout << std::endl;
145}
146
147
148int main () {
149
150 std::cout << "STL generic algorithms -- that transform sequences"
151 << std::endl;
152
156
157 std::cout << "End generic transform algorithms example" << std::endl;
158
159 return 0;
160}
161
162
163
164
165
166
167
#define n
Definition 17.names.cpp:130
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg3.cpp:45
void partial_sum_example()
Definition alg6.cpp:101
int square(int n)
Definition alg6.cpp:43
void transform_example()
Definition alg6.cpp:61
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg6.cpp:40
void adjacent_difference_example()
Definition alg6.cpp:125
int main()
Definition alg6.cpp:148
int operator()()
Definition alg1.cpp:53
iotaGen(int iv)
Definition alg6.cpp:50