Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg1.cpp
Go to the documentation of this file.
1
30#include <vector> // for vector
31#include <list> // for list
32#include <algorithm> // for copy(), fill(), generate(), swap()
33#include <string> // for string
34#include <iostream> // for cout
35#include <iterator> // for ostream_iterator
36#include <sstream> // for stringstream
37
38#include <examples.h>
39
40
41typedef std::ostream_iterator<int, char,
42 std::char_traits<char> > int_ostrm_iter;
43typedef std::ostream_iterator<std::string, char,
44 std::char_traits<char> > str_ostrm_iter;
45typedef std::ostream_iterator<char, char,
46 std::char_traits<char> > char_ostrm_iter;
47
49{
50public:
51 iotaGen (int iv) : current (iv)
52 { }
54 return current++;
55 }
56private:
57 int current;
58};
59
60
61// Illustrate the use of the fill and fill_n functions.
63{
64 std::cout << "Illustrate fill function\n";
65
66 // Example 1, fill an array with initial values
67 char buffer [100];
68 char *bufferp = buffer;
69
70 std::fill (bufferp, (bufferp + 100), '\0');
71 std::fill_n (bufferp, 10, 'x');
72
73 std::cout << buffer << '\n';
74
75 // Example 2, use fill to initialize a std::list.
76 std::list<std::string, std::allocator<std::string> > aList;
77
78 std::fill_n (std::inserter (aList, aList.begin ()), 10, "empty");
79 std::copy (aList.begin (), aList.end (),
80 str_ostrm_iter (std::cout, " "));
81
82 std::cout << '\n';
83
84 // Example 3, use fill to overwrite values in a std::list.
85 std::fill (aList.begin (), aList.end (), "full");
86 std::copy (aList.begin (), aList.end (),
87 str_ostrm_iter (std::cout, " "));
88
89 std::cout << '\n';
90
91 // Example 4, fill in a portion of a std::list.
92 std::vector<int, std::allocator<int> > iVec (10);
93
94 std::generate (iVec.begin (), iVec.end (), iotaGen (1));
95 std::vector<int, std::allocator<int> >::iterator
96 seven = std::find (iVec.begin (), iVec.end (), 7);
97 std::fill (iVec.begin (), seven, 0);
98 std::copy (iVec.begin (), iVec.end (), int_ostrm_iter (std::cout));
99
100 std::cout << '\n';
101}
102
103
104// Illustrate the use of the copy function.
106{
107 std::cout << "Illustrate copy function\n";
108
109 // Example 1, a simple copy.
110 const char source[] = "reprise";
111 const char surpass[] = "surpass";
112 char buffer [120];
113 char *bufferp = buffer;
114
115 std::copy (&source [0],
116 source + std::char_traits<char>::length (source) + 1, bufferp);
117
118 // Example 2, self copies.
119 *std::copy (bufferp + 2,
120 bufferp + std::char_traits<char>::length (buffer),
121 bufferp) = '\0';
122
123 std::size_t buflen = std::char_traits<char>::length (buffer) + 1;
124 std::copy_backward (bufferp,
125 bufferp + buflen, bufferp + buflen + 3);
126 std::copy (&surpass [0], surpass + 3, bufferp);
127
128 // Example 3, copy to output.
129 std::copy (bufferp, bufferp + std::char_traits<char>::length (buffer),
130 char_ostrm_iter (std::cout));
131
132 std::cout << '\n';
133
134 // Example 4, use copy to convert type.
135 std::list<char, std::allocator<char> > char_list;
136 const char big[] = "big ";
137 char buffer2 [200];
138 char *buffer2p = buffer2;
139
140 std::copy (bufferp, bufferp + std::char_traits<char>::length (buffer),
141 std::inserter (char_list, char_list.end ()));
142 std::copy (&big [0], big + 4,
143 std::inserter (char_list, char_list.begin ()));
144 *std::copy (char_list.begin (),
145 char_list.end (), buffer2p) = '\0';
146
147 std::cout << buffer2 << '\n';
148}
149
150
151std::string generateLabel ()
152{
153 // Generate a label std::string of the form L_ddd.
154 static int lastLabel = 0;
155 std::ostringstream ost;
156
157 ost << "L_" << lastLabel++;
158
159 return ost.str ();
160}
161
162
163// Illustrate the use of the generate and genrate_n functions.
165{
166 std::cout << "Illustrate generate algorithm\n";
167
168 // Example 1, generate a std::list of label numbers.
169 std::list<std::string, std::allocator<std::string> > labelList;
170
171 std::generate_n (std::inserter (labelList, labelList.begin ()),
172 4, generateLabel);
173 std::copy (labelList.begin (), labelList.end (),
174 str_ostrm_iter (std::cout, " "));
175
176 std::cout << '\n';
177
178 // Example 2, generate an arithmetic progression.
179 std::vector<int, std::allocator<int> > iVec (10);
180
181 std::generate (iVec.begin (), iVec.end (), iotaGen (2));
182 std::generate_n (iVec.begin (), 5, iotaGen (7));
183 std::copy (iVec.begin (), iVec.end (), int_ostrm_iter (std::cout, " "));
184
185 std::cout << '\n';
186}
187
188
189// Illustrate the use of the algorithm swap_ranges.
191{
192 std::cout << "Illustrate swap_ranges algorithm\n";
193
194 // First make two parallel sequences.
195 int data[] = {12, 27, 14, 64}, *datap = data;
196 std::vector<int, std::allocator<int> > aVec (4);
197
198 std::generate (aVec.begin (), aVec.end (), iotaGen (1));
199
200 // Illustrate swap and iter_swap.
201 std::swap (data [0], data [2]);
202 std::copy (data, data + 4, int_ostrm_iter (std::cout, " "));
203
204 std::cout << '\n';
205
206 std::vector<int, std::allocator<int> >::iterator last =
207 aVec.end ();
208
209 --last;
210
211 std::iter_swap (aVec.begin (), last);
212 std::copy (aVec.begin (), aVec.end (),
213 int_ostrm_iter (std::cout, " "));
214
215 std::cout << '\n';
216
217 // Now swap the entire sequence.
218 std::swap_ranges (aVec.begin (), aVec.end (), datap);
219 std::copy (data, data+4, int_ostrm_iter (std::cout, " "));
220
221 std::cout << '\n';
222
223 std::copy (aVec.begin (), aVec.end (),
224 int_ostrm_iter (std::cout, " "));
225
226 std::cout << '\n';
227}
228
229
230int main ()
231{
232 std::cout << "STL generic algorithms -- initialization algorithms\n";
233
234 fill_example ();
235 copy_example ();
237 swap_example ();
238
239 std::cout << "End of initialization tests\n";
240
241 return 0;
242}
#define last
Definition 17.names.cpp:487
#define buffer
Definition 17.names.cpp:453
const char *const data[]
std::ostream_iterator< int, char, std::char_traits< char > > int_ostrm_iter
Definition alg1.cpp:42
std::ostream_iterator< std::string, char, std::char_traits< char > > str_ostrm_iter
Definition alg1.cpp:44
void generate_example()
Definition alg1.cpp:164
std::ostream_iterator< char, char, std::char_traits< char > > char_ostrm_iter
Definition alg1.cpp:46
void swap_example()
Definition alg1.cpp:190
std::string generateLabel()
Definition alg1.cpp:151
void copy_example()
Definition alg1.cpp:105
int main()
Definition alg1.cpp:230
void fill_example()
Definition alg1.cpp:62
int operator()()
Definition alg1.cpp:53
iotaGen(int iv)
Definition alg1.cpp:51