From 23de610bbd7aff176a866a2960a3f0d69a3351db Mon Sep 17 00:00:00 2001 From: Emil Enchev <43370348+EmilEnchev@users.noreply.github.com> Date: Sat, 15 Jun 2019 13:18:08 +0300 Subject: [PATCH 1/3] Example any_of Fix example if you do not like something in my English. --- docs/standard-library/algorithm-functions.md | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/docs/standard-library/algorithm-functions.md b/docs/standard-library/algorithm-functions.md index 9b2b31f092b..273b763dc1b 100644 --- a/docs/standard-library/algorithm-functions.md +++ b/docs/standard-library/algorithm-functions.md @@ -203,6 +203,52 @@ The template function returns **true** only if, for some `N` in the range `[0, last - first)`, the predicate `comp(*(first + N))` is true. +### Example + +```cpp +// alg_any_of.cpp +// compile with: /EHsc +#include +#include +#include + +// Returns whether element is even +bool is_even(int elem) +{ + return elem % 2 == 0; +} + +int main() +{ + using namespace std; + list L; + list ::iterator Iter; + list ::iterator result1, result2; + + L.push_back(51); + L.push_back(41); + L.push_back(11); + L.push_back(21); + L.push_back(20); + + cout << "L = ( "; + for (Iter = L.begin(); Iter != L.end(); Iter++) + cout << *Iter << " "; + cout << ")" << endl; + + // Check if there is an even elememt in L. + if (any_of(L.begin(), L.end(), is_even)) + cout << "There is even element in L\n"; + else + cout << "There are not even elements in L.\n"; +} +``` + +```Output +L = ( 51 41 11 21 20 ) +There is even element in L +``` + ## binary_search 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. From aed3b364efe3fcaf1a657249718df5a241c12676 Mon Sep 17 00:00:00 2001 From: Colin Robertson Date: Mon, 17 Jun 2019 13:35:09 -0700 Subject: [PATCH 2/3] Update algorithm-functions.md Simplify example. Update naming and formatting per style. --- docs/standard-library/algorithm-functions.md | 43 ++++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/docs/standard-library/algorithm-functions.md b/docs/standard-library/algorithm-functions.md index 273b763dc1b..f8d72c8e1d3 100644 --- a/docs/standard-library/algorithm-functions.md +++ b/docs/standard-library/algorithm-functions.md @@ -212,41 +212,30 @@ The template function returns **true** only if, for some `N` in the range #include #include -// Returns whether element is even -bool is_even(int elem) -{ - return elem % 2 == 0; -} int main() { - using namespace std; - list L; - list ::iterator Iter; - list ::iterator result1, result2; - - L.push_back(51); - L.push_back(41); - L.push_back(11); - L.push_back(21); - L.push_back(20); - - cout << "L = ( "; - for (Iter = L.begin(); Iter != L.end(); Iter++) - cout << *Iter << " "; - cout << ")" << endl; - - // Check if there is an even elememt in L. - if (any_of(L.begin(), L.end(), is_even)) - cout << "There is even element in L\n"; - else - cout << "There are not even elements in L.\n"; + using namespace std; + + list li { 51, 41, 11, 21, 20 }; + + cout << "li = ( "; + for (auto const& el : li) + cout << el << " "; + cout << ")" << endl; + + // Check if there is an even elememt in li. + auto is_even = [](int const elem){ return !(elem % 2); }; + if (any_of(li.begin(), li.end(), is_even)) + cout << "There's an even element in li.\n"; + else + cout << "There are no even elements in li.\n"; } ``` ```Output L = ( 51 41 11 21 20 ) -There is even element in L +There's an even element in li. ``` ## binary_search From 6d04435cf18ed67bddac4037d90adf552779875f Mon Sep 17 00:00:00 2001 From: Colin Robertson Date: Mon, 17 Jun 2019 13:37:59 -0700 Subject: [PATCH 3/3] Update algorithm-functions.md Delete stray space. --- docs/standard-library/algorithm-functions.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/standard-library/algorithm-functions.md b/docs/standard-library/algorithm-functions.md index f8d72c8e1d3..b00d59a418d 100644 --- a/docs/standard-library/algorithm-functions.md +++ b/docs/standard-library/algorithm-functions.md @@ -212,7 +212,6 @@ The template function returns **true** only if, for some `N` in the range #include #include - int main() { using namespace std;