Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg2.cpp
Go to the documentation of this file.
1
30#include <vector>
31#include <list>
32#include <algorithm>
33#include <string>
34#include <cstring>
35#include <iostream>
36
37#include <examples.h>
38
39
40void split (const std::string & text, const std::string & separators,
41 std::list<std::string, std::allocator<std::string> > & words) {
42
43 const std::string::size_type n = text.length ();
44 std::string::size_type start = text.find_first_not_of (separators);
45
46 while (start < n) {
47 std::string::size_type stop = text.find_first_of (separators, start);
48 if (stop > n)
49 stop = n;
50 words.push_back (text.substr (start, stop - start));
51 start = text.find_first_not_of (separators, stop+1);
52 }
53}
54
55
56bool isLeapYear (unsigned int year)
57{
58 return !(year % 1000 && year % 100 && year % 4);
59}
60
61
62// Illustrate use of the find function.
63void find_test () {
64
65 std::cout << "Test of algorithm find\n";
66
67 const int vintageYears[] = { 1967, 1972, 1974, 1980, 1995 };
68
69 const int* start = vintageYears;
70 const int* stop = vintageYears
71 + sizeof vintageYears / sizeof *vintageYears;
72 const int* where = std::find_if (start, stop, isLeapYear);
73
74 if (where != stop)
75 std::cout << "first vintage leap year is " << *where << '\n';
76 else
77 std::cout << "no vintage leap years" << '\n';
78
79 where = std::find (start, stop, 1995);
80
81 if (where != stop)
82 std::cout << "1995 is position " << where - start
83 << " in sequence" << '\n';
84 else
85 std::cout << "1995 does not occur in sequence\n";
86}
87
88
90
91 std::cout << "Test of algorithm find adjacent\n";
92
93 const char text[] = "The bookkeeper carefully opened the door";
94
95 const char* start = text;
96 const char* stop = text + sizeof text;
97 const char* where = start;
98
99 std::cout << "In the text " << text << '\n';
100
101 while ((where = std::adjacent_find (where, stop)) != stop) {
102 std::cout << "double " << *where << " in position "
103 << where-start << '\n';
104 ++where;
105 }
106}
107
108
109// Illustrate the use of the search function.
110void search_test () {
111
112 std::cout << "Test of algorithm search\n";
113
114 char base[] = "aspirations";
115 char text[] = "ration";
116
117 const char* const where = std::search (base, base+std::strlen (base),
118 text, text+std::strlen (text));
119
120 if (*where != '\0')
121 std::cout << "substring begins in position "
122 << where - base << '\n';
123 else
124 std::cout << "substring does not occur in text\n";
125}
126
127
128// Illustrate use of max_element and min_element algorithms.
130
131 std::cout << "Test of max and min algorithms \n";
132
133 // make a std::vector of "random" numbers between 0 and 99.
134 const int rn[] = {
135 83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
136 62, 27, 90, 59, 63, 26, 40, 26, 72, 36,
137 11, 68, 67, 29, 82
138 };
139
140 std::vector<int, std::allocator<int> >
141 numbers (rn, rn + (sizeof rn/sizeof *rn));
142
143 // Print the maximum and minimum.
144 std::vector<int, std::allocator<int> >::iterator max =
145 std::max_element (numbers.begin (), numbers.end ());
146
147 std::cout << "largest value was " << *max << '\n';
148
149 std::vector<int, std::allocator<int> >::iterator min =
150 std::min_element (numbers.begin (), numbers.end ());
151
152 std::cout << "smallest value was " << *min << '\n';
153
154 // Example using std::strings.
155 std::string text = "it was the best of times, "\
156 "it was the worst of times.";
157 std::list<std::string, std::allocator<std::string> >words;
158
159 split (text, " ., !:;", words);
160
161 std::cout << "The smallest word is "
162 << * std::min_element (words.begin (), words.end ())
163 << " and the largest word is "
164 << * std::max_element (words.begin (), words.end ())
165 << '\n';
166}
167
168
169// Illustrate the use of the mismatch function.
170void mismatch_test (const char * a, const char * b)
171{
172 std::pair<const char *, const char *> differPositions (0, 0);
173 const char *aDiffPos;
174 const char *bDiffPos;
175
176 if (std::strlen (a) < std::strlen (b)) {
177 differPositions = std::mismatch (a, a + std::strlen (a), b);
178 aDiffPos = differPositions.first;
179 bDiffPos = differPositions.second;
180 }
181 else {
182 differPositions = std::mismatch (b, b + std::strlen (b), a);
183 aDiffPos = differPositions.second;
184 bDiffPos = differPositions.first;
185 }
186
187 std::cout << "string " << a;
188
189 if (*aDiffPos == *bDiffPos)
190 std::cout << " is equal to ";
191 else if (*aDiffPos < *bDiffPos)
192 std::cout << " is less than ";
193 else
194 std::cout << " is greater than ";
195
196 std::cout << b << '\n';
197}
198
199
200int main ()
201{
202 std::cout << "STL generic algorithms -- Searching Algorithms\n";
203
204 find_test ();
206 search_test ();
208 mismatch_test ("goody", "goody");
209 mismatch_test ("good", "goody");
210 mismatch_test ("goody", "good");
211 mismatch_test ("good", "fred");
212 mismatch_test ("fred", "good");
213
214 std::cout << "End of search algorithms test program\n";
215
216 return 0;
217}
#define start
Definition 17.names.cpp:547
#define a
Definition 17.names.cpp:109
#define b
Definition 17.names.cpp:110
#define n
Definition 17.names.cpp:130
void find_test()
Definition alg2.cpp:63
void find_adjacent_test()
Definition alg2.cpp:89
bool isLeapYear(unsigned int year)
Definition alg2.cpp:56
void split(const std::string &text, const std::string &separators, std::list< std::string, std::allocator< std::string > > &words)
Definition alg2.cpp:40
void search_test()
Definition alg2.cpp:110
void mismatch_test(const char *a, const char *b)
Definition alg2.cpp:170
void max_min_example()
Definition alg2.cpp:129
int main()
Definition alg2.cpp:200
#define min(a, b)
#define max(a, b)