Skip to content

Commit f3acfe5

Browse files
author
Colin Robertson
authored
Merge pull request #1092 from EmilEnchev/patch-2
Example of all_of
2 parents 0851eae + 94f72b9 commit f3acfe5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/standard-library/algorithm-functions.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,41 @@ Returns **true** if the condition is detected at each element in the indicated r
172172
173173
The template function returns **true** only if, for each `N` in the range `[0,Last - first)`, the predicate `comp(*(_First + N))` is **true**.
174174
175+
### Example
176+
177+
```cpp
178+
// alg_all_of.cpp
179+
// compile with: /EHsc
180+
#include <list>
181+
#include <algorithm>
182+
#include <iostream>
183+
184+
int main()
185+
{
186+
using namespace std;
187+
188+
list<int> li { 50, 40, 10, 20, 20 };
189+
list<int>::iterator iter;
190+
191+
cout << "li = ( ";
192+
for (iter = li.begin(); iter != li.end(); iter++)
193+
cout << *iter << " ";
194+
cout << ")" << endl;
195+
196+
// Check if all elements in li are even.
197+
auto is_even = [](int elem){ return !(elem % 2); };
198+
if (all_of(li.begin(), li.end(), is_even))
199+
cout << "All the elements are even numbers.\n";
200+
else
201+
cout << "Not all the elements are even numbers.\n";
202+
}
203+
```
204+
205+
```Output
206+
L = ( 50 40 10 20 20 )
207+
All the elements are even numbers.
208+
```
209+
175210
## <a name="any_of"></a> any_of
176211

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

0 commit comments

Comments
 (0)