????c++11?????????Щ????????????Щ?????????????????д???????????????????о??Щ??????????????????????????????????????????ο?http://en.cppreference.com/w/cpp/algorithm??
???????????????????????ж????all_of??any_of??none_of??
????template< class InputIt?? class UnaryPredicate >
????bool all_of( InputIt first?? InputIt last?? UnaryPredicate p );
????template< class InputIt?? class UnaryPredicate >
????bool any_of( InputIt first?? InputIt last?? UnaryPredicate p );
????template< class InputIt?? class UnaryPredicate >
????bool none_of( InputIt first?? InputIt last?? UnaryPredicate p );
????all_of:???????[first?? last)????????е????????????ж??p?????е?????????????????true????????false??
????any_of?????????[first?? last)?????????????????????????ж??p????????????????????????true????????true??
????none_of?????????[first?? last)????????е??????????????ж??p?????е???????????????????true????????false??
???????????????????????
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1?? 3?? 5?? 7?? 9 };
auto isEven = [](int i){return i % 2 != 0;
bool isallOdd = std::all_of(v.begin()?? v.end()?? isEven);
if (isallOdd)
cout << "all is odd" << endl;
bool isNoneEven = std::none_of(v.begin()?? v.end()?? isEven);
if (isNoneEven)
cout << "none is even" << endl;
vector<int> v1 = { 1?? 3?? 5?? 7?? 8?? 9 };
bool anyof = std::any_of(v1.begin()?? v1.end()?? isEven);
if (anyof)
cout << "at least one is even" << endl;
}
?????????
????all is odd
????none is odd
????at least one is even
???????????????????????find_if_not??????????find_if???????????????????????????????find_if????????find_if_not?????????????ж??????????ж?????????????????find_if_not??????????д?????ж????????????????á???????????????÷???
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1?? 3?? 5?? 7?? 9??4 };
auto isEven = [](int i){return i % 2 == 0;};
auto firstEven = std::find_if(v.begin()?? v.end()?? isEven);
if (firstEven!=v.end())
cout << "the first even is " <<* firstEven << endl;
//??find_if???????????????????д???????????ж??
auto isNotEven = [](int i){return i % 2 != 0;};
auto firstOdd = std::find_if(v.begin()?? v.end()??isNotEven);
if (firstOdd!=v.end())
cout << "the first odd is " <<* firstOdd << endl;
//??find_if_not??????????????????????ж??
auto odd = std::find_if_not(v.begin()?? v.end()?? isEven);
if (odd!=v.end())
cout << "the first odd is " <<* odd << endl;
}
???????????
????the first even is 4
????the first odd is 1
????the first odd is 1