Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
alg5.cpp
Go to the documentation of this file.
1
30#include <algorithm> // for copy
31#include <functional> // for multiplies
32#include <iterator> // for ostream_iterator
33#include <list> // for list
34#include <numeric> // for accumulate
35#include <iostream> // for cout, endl
36
37#include <examples.h>
38
39typedef std::ostream_iterator<int, char, std::char_traits<char> >
41
42// Forward declarations.
43bool isVowel (char);
44void count_example ();
45void accumulate_example ();
47void equal_example ();
48
49
50bool isVowel (char c) {
51
52 switch (c)
53 {
54 case 'a': case 'A':
55 case 'e': case 'E':
56 case 'i': case 'I':
57 case 'o': case 'O':
58 case 'u': case 'U':
59 return true;
60 }
61 return false;
62}
63
64
65// Illustrate the use of the count function.
67
68 int ecount = 0;
69 int vowelCount = 0;
70
71 char text[] = "Now is the time to begin";
72
73#ifdef _RWSTD_STRICT_ANSI
74
75 ecount = std::count (text, text + sizeof text, 'e');
76 vowelCount = std::count_if (text, text + sizeof text, isVowel);
77
78#else
79
80 std::count (text, text + sizeof text, 'e', ecount);
81 std::count_if (text, text + sizeof text, isVowel, vowelCount);
82
83#endif // _RWSTD_STRICT_ANSI
84
85 std::cout << "There are " << ecount
86 << " letter e's " << std::endl
87 << "and " << vowelCount
88 << " vowels in the text:" << text
89 << std::endl;
90}
91
92
93// Add n to 1 to std::list.
94std::list<int, std::allocator<int> >&
95intReplicate (std::list<int, std::allocator<int> >& nums, int n) {
96
97 while (n)
98 nums.push_back (n--);
99 return nums;
100}
101
102
103// Illustrate the use of the accumulate function.
105
106 int numbers[] = { 1, 2, 3, 4, 5 };
107 int sum = std::accumulate (numbers, numbers+5, 0);
108 int product = std::accumulate (numbers,
109 numbers+5, 1, std::multiplies<int> ());
110
111 std::cout << "The sum of the first five numbers is "
112 << sum
113 << std::endl;
114 std::cout << "The product of the first five numbers is "
115 << product
116 << std::endl;
117
118 // Example with different types for init.
119 std::list<int, std::allocator<int> > nums;
120 nums = std::accumulate (numbers, numbers+5, nums, intReplicate);
121
122 std::copy (nums.begin (), nums.end (), ostrm_iter_type (std::cout, " "));
123
124 std::cout << std::endl;
125}
126
127
128// Illustrate the use of the inner_product function.
130
131 int a[] = { 4, 3, -2 };
132 int b[] = { 7, 3, 2 };
133
134 // Example 1, simple inner product.
135 int in1 = std::inner_product (a, a+3, b, 0);
136 std::cout << "Inner product is " << in1 << std::endl;
137
138 // Example 2, using different operations.
139 bool anyequal = std::inner_product (a, a+3, b, true,
140 std::logical_or<bool> (),
141 std::equal_to<int> ());
142
143 std::cout << "any equal? " << anyequal << std::endl;
144}
145
146
147// Illustrate the use of the equal function.
149
150 int a[] = { 4, 5, 3 };
151 int b[] = { 4, 3, 3 };
152 int c[] = { 4, 5, 3 };
153
154 std::cout << "a = b is:" << std::equal (a, a+3, b) << std::endl;
155 std::cout << "a = c is:" << std::equal (a, a+3, c) << std::endl;
156 std::cout << "a pair-wise-greater_equal b is"
157 << std::equal (a, a+3, b, std::greater_equal<int> ())
158 << std::endl;
159}
160
161
162// Illustrate the use of the lexical_comparison function.
164
165 char wordOne[] = "everything";
166 char wordTwo[] = "everybody";
167
168 std::cout << "compare everybody to everything "
169 << std::lexicographical_compare (wordTwo,
170 wordTwo + sizeof wordTwo,
171 wordOne,
172 wordOne + sizeof wordOne)
173 << std::endl;
174
175 int a[] = { 3, 4, 5, 2 };
176 int b[] = { 3, 4, 5 };
177 int c[] = { 3, 5 };
178
179 std::cout << "compare a to b: "
180 << std::lexicographical_compare (a, a+4, b, b+3) << std::endl;
181 std::cout << "compare a to c: "
182 << std::lexicographical_compare (a, a+4, c, c+2) << std::endl;
183}
184
185int main () {
186
187 std::cout << "STL generic algorithms -- "\
188 << "algorithms that produce scalar results" << std::endl;
189
190 count_example ();
193 equal_example ();
195
196 std::cout << "End of scalar algorithms test" << std::endl;
197
198 return 0;
199}
#define a
Definition 17.names.cpp:109
#define c
Definition 17.names.cpp:111
#define b
Definition 17.names.cpp:110
#define n
Definition 17.names.cpp:130
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg3.cpp:45
void accumulate_example()
Definition alg5.cpp:104
void equal_example()
Definition alg5.cpp:148
void inner_product_example()
Definition alg5.cpp:129
std::list< int, std::allocator< int > > & intReplicate(std::list< int, std::allocator< int > > &nums, int n)
Definition alg5.cpp:95
void count_example()
Definition alg5.cpp:66
bool isVowel(char)
Definition alg5.cpp:50
std::ostream_iterator< int, char, std::char_traits< char > > ostrm_iter_type
Definition alg5.cpp:40
void lexical_comparison_example()
Definition alg5.cpp:163
int main()
Definition alg5.cpp:185