Skip to content

Commit 23de610

Browse files
authored
Example any_of
Fix example if you do not like something in my English.
1 parent a2433fe commit 23de610

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

docs/standard-library/algorithm-functions.md

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

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

206+
### Example
207+
208+
```cpp
209+
// alg_any_of.cpp
210+
// compile with: /EHsc
211+
#include <list>
212+
#include <algorithm>
213+
#include <iostream>
214+
215+
// Returns whether element is even
216+
bool is_even(int elem)
217+
{
218+
return elem % 2 == 0;
219+
}
220+
221+
int main()
222+
{
223+
using namespace std;
224+
list <int> L;
225+
list <int>::iterator Iter;
226+
list <int>::iterator result1, result2;
227+
228+
L.push_back(51);
229+
L.push_back(41);
230+
L.push_back(11);
231+
L.push_back(21);
232+
L.push_back(20);
233+
234+
cout << "L = ( ";
235+
for (Iter = L.begin(); Iter != L.end(); Iter++)
236+
cout << *Iter << " ";
237+
cout << ")" << endl;
238+
239+
// Check if there is an even elememt in L.
240+
if (any_of(L.begin(), L.end(), is_even))
241+
cout << "There is even element in L\n";
242+
else
243+
cout << "There are not even elements in L.\n";
244+
}
245+
```
246+
247+
```Output
248+
L = ( 51 41 11 21 20 )
249+
There is even element in L
250+
```
251+
206252
## <a name="binary_search"></a> binary_search
207253

208254
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)