Skip to content

Commit e1cd17e

Browse files
authored
Example of all_of
New example of all_of
1 parent a2433fe commit e1cd17e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/standard-library/algorithm-functions.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,54 @@ Returns **true** if the condition is detected at each element in the indicated r
170170
171171
The template function returns **true** only if, for each `N` in the range `[0,Last - first)`, the predicate `comp(*(_First + N))` is **true**.
172172
173+
### Example
174+
175+
```cpp
176+
// alg_all_of.cpp
177+
// compile with: /EHsc
178+
#include <list>
179+
#include <algorithm>
180+
#include <iostream>
181+
182+
// Returns whether element is even
183+
bool is_even(int elem)
184+
{
185+
186+
return elem % 2 == 0;
187+
}
188+
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";
212+
}
213+
```
214+
215+
```Output
216+
L = ( 50 40 10 20 20 )
217+
All the elements are even numbers.
218+
```
219+
220+
173221
## <a name="any_of"></a> any_of
174222

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

0 commit comments

Comments
 (0)