40void split (
const std::string & text,
const std::string & separators,
41 std::list<std::string, std::allocator<std::string> > & words) {
43 const std::string::size_type
n = text.length ();
44 std::string::size_type
start = text.find_first_not_of (separators);
47 std::string::size_type stop = text.find_first_of (separators,
start);
50 words.push_back (text.substr (
start, stop -
start));
51 start = text.find_first_not_of (separators, stop+1);
58 return !(year % 1000 && year % 100 && year % 4);
65 std::cout <<
"Test of algorithm find\n";
67 const int vintageYears[] = { 1967, 1972, 1974, 1980, 1995 };
69 const int*
start = vintageYears;
70 const int* stop = vintageYears
71 +
sizeof vintageYears /
sizeof *vintageYears;
75 std::cout <<
"first vintage leap year is " << *where <<
'\n';
77 std::cout <<
"no vintage leap years" <<
'\n';
79 where = std::find (
start, stop, 1995);
82 std::cout <<
"1995 is position " << where -
start
83 <<
" in sequence" <<
'\n';
85 std::cout <<
"1995 does not occur in sequence\n";
91 std::cout <<
"Test of algorithm find adjacent\n";
93 const char text[] =
"The bookkeeper carefully opened the door";
95 const char*
start = text;
96 const char* stop = text +
sizeof text;
97 const char* where =
start;
99 std::cout <<
"In the text " << text <<
'\n';
101 while ((where = std::adjacent_find (where, stop)) != stop) {
102 std::cout <<
"double " << *where <<
" in position "
103 << where-
start <<
'\n';
112 std::cout <<
"Test of algorithm search\n";
114 char base[] =
"aspirations";
115 char text[] =
"ration";
117 const char*
const where = std::search (
base,
base+std::strlen (
base),
118 text, text+std::strlen (text));
121 std::cout <<
"substring begins in position "
122 << where -
base <<
'\n';
124 std::cout <<
"substring does not occur in text\n";
131 std::cout <<
"Test of max and min algorithms \n";
135 83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
136 62, 27, 90, 59, 63, 26, 40, 26, 72, 36,
140 std::vector<int, std::allocator<int> >
141 numbers (rn, rn + (
sizeof rn/
sizeof *rn));
144 std::vector<int, std::allocator<int> >::iterator
max =
145 std::max_element (numbers.begin (), numbers.end ());
147 std::cout <<
"largest value was " << *
max <<
'\n';
149 std::vector<int, std::allocator<int> >::iterator
min =
150 std::min_element (numbers.begin (), numbers.end ());
152 std::cout <<
"smallest value was " << *
min <<
'\n';
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;
159 split (text,
" ., !:;", words);
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 ())
172 std::pair<const char *, const char *> differPositions (0, 0);
173 const char *aDiffPos;
174 const char *bDiffPos;
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;
182 differPositions = std::mismatch (
b,
b + std::strlen (
b),
a);
183 aDiffPos = differPositions.second;
184 bDiffPos = differPositions.first;
187 std::cout <<
"string " <<
a;
189 if (*aDiffPos == *bDiffPos)
190 std::cout <<
" is equal to ";
191 else if (*aDiffPos < *bDiffPos)
192 std::cout <<
" is less than ";
194 std::cout <<
" is greater than ";
196 std::cout <<
b <<
'\n';
202 std::cout <<
"STL generic algorithms -- Searching Algorithms\n";
214 std::cout <<
"End of search algorithms test program\n";
void find_adjacent_test()
bool isLeapYear(unsigned int year)
void split(const std::string &text, const std::string &separators, std::list< std::string, std::allocator< std::string > > &words)
void mismatch_test(const char *a, const char *b)