Skip to content

Commit 40ab452

Browse files
author
Colin Robertson
authored
Merge pull request #1094 from EmilEnchev/patch-4
Example any_of
2 parents 13d1019 + 6d04435 commit 40ab452

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/standard-library/algorithm-functions.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,40 @@ The template function returns **true** only if, for some `N` in the range
240240

241241
`[0, last - first)`, the predicate `comp(*(first + N))` is true.
242242

243+
### Example
244+
245+
```cpp
246+
// alg_any_of.cpp
247+
// compile with: /EHsc
248+
#include <list>
249+
#include <algorithm>
250+
#include <iostream>
251+
252+
int main()
253+
{
254+
using namespace std;
255+
256+
list<int> li { 51, 41, 11, 21, 20 };
257+
258+
cout << "li = ( ";
259+
for (auto const& el : li)
260+
cout << el << " ";
261+
cout << ")" << endl;
262+
263+
// Check if there is an even elememt in li.
264+
auto is_even = [](int const elem){ return !(elem % 2); };
265+
if (any_of(li.begin(), li.end(), is_even))
266+
cout << "There's an even element in li.\n";
267+
else
268+
cout << "There are no even elements in li.\n";
269+
}
270+
```
271+
272+
```Output
273+
L = ( 51 41 11 21 20 )
274+
There's an even element in li.
275+
```
276+
243277
## <a name="binary_search"></a> binary_search
244278

245279
Tests whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate.

0 commit comments

Comments
 (0)