Skip to content

Commit 94f72b9

Browse files
author
Colin Robertson
authored
Update algorithm-functions.md
Simplify alg_all_of.cpp example slightly. Remove unused variables. Update formatting and identifiers per style.
1 parent e1cd17e commit 94f72b9

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

docs/standard-library/algorithm-functions.md

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,24 @@ The template function returns **true** only if, for each `N` in the range `[0,La
179179
#include <algorithm>
180180
#include <iostream>
181181
182-
// Returns whether element is even
183-
bool is_even(int elem)
182+
int main()
184183
{
184+
using namespace std;
185185
186-
return elem % 2 == 0;
187-
}
186+
list<int> li { 50, 40, 10, 20, 20 };
187+
list<int>::iterator iter;
188188
189-
int main()
190-
{
191-
using namespace std;
192-
list <int> L;
193-
list <int>::iterator Iter;
194-
list <int>::iterator result1, result2;
195-
196-
L.push_back(50);
197-
L.push_back(40);
198-
L.push_back(10);
199-
L.push_back(20);
200-
L.push_back(20);
201-
202-
cout << "L = ( ";
203-
for (Iter = L.begin(); Iter != L.end(); Iter++)
204-
cout << *Iter << " ";
205-
cout << ")" << endl;
206-
207-
// Check if all elements in L are even.
208-
if (all_of(L.begin(), L.end(), is_even))
209-
cout << "All the elements are even numbers.\n";
210-
else
211-
cout << "Not all the elements are even numbers.\n";
189+
cout << "li = ( ";
190+
for (iter = li.begin(); iter != li.end(); iter++)
191+
cout << *iter << " ";
192+
cout << ")" << endl;
193+
194+
// Check if all elements in li are even.
195+
auto is_even = [](int elem){ return !(elem % 2); };
196+
if (all_of(li.begin(), li.end(), is_even))
197+
cout << "All the elements are even numbers.\n";
198+
else
199+
cout << "Not all the elements are even numbers.\n";
212200
}
213201
```
214202

@@ -217,7 +205,6 @@ L = ( 50 40 10 20 20 )
217205
All the elements are even numbers.
218206
```
219207

220-
221208
## <a name="any_of"></a> any_of
222209

223210
Returns **true** when a condition is present at least once in the specified range of elements.

0 commit comments

Comments
 (0)