Skip to content

Example of all_of #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/standard-library/algorithm-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,41 @@ Returns **true** if the condition is detected at each element in the indicated r

The template function returns **true** only if, for each `N` in the range `[0,Last - first)`, the predicate `comp(*(_First + N))` is **true**.

### Example

```cpp
// alg_all_of.cpp
// compile with: /EHsc
#include <list>
#include <algorithm>
#include <iostream>

int main()
{
using namespace std;

list<int> li { 50, 40, 10, 20, 20 };
list<int>::iterator iter;

cout << "li = ( ";
for (iter = li.begin(); iter != li.end(); iter++)
cout << *iter << " ";
cout << ")" << endl;

// Check if all elements in li are even.
auto is_even = [](int elem){ return !(elem % 2); };
if (all_of(li.begin(), li.end(), is_even))
cout << "All the elements are even numbers.\n";
else
cout << "Not all the elements are even numbers.\n";
}
```

```Output
L = ( 50 40 10 20 20 )
All the elements are even numbers.
```

## <a name="any_of"></a> any_of

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