template<classInputIterator,classUnaryPredicate>InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred){while(first!=last){if(pred(*first))return first;++first;}return last;}
案例:
// find_if example#include<iostream>// std::cout#include<algorithm>// std::find_if#include<vector>// std::vectorbool IsOdd (int i){return((i%2)==1);}int main (){std::vector<int> myvector;myvector.push_back(10);myvector.push_back(25);myvector.push_back(40);myvector.push_back(55);std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);std::cout <<"The first odd value is "<<*it <<'\n';return0;}Output:
The first odd value is 25
2.std::find_if_not
原型:
template<classInputIterator,classUnaryPredicate>InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);
template<classInputIterator,classUnaryPredicate>InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred){while(first!=last){if(!pred(*first))return first;++first;}return last;}
案例:
// find_if_not example#include<iostream>// std::cout#include<algorithm>// std::find_if_not#include<array>// std::arrayint main (){std::array<int,5> foo ={1,2,3,4,5};std::array<int,5>::iterator it =std::find_if_not (foo.begin(), foo.end(),[](int i){return i%2;});std::cout <<"The first even value is "<<*it <<'\n';return0;}Output:
The first even value is 2
3.std::for_each
原型:
template<classInputIterator,classFunction>Function for_each (InputIterator first, InputIterator last, Function fn);
參數:
InputIterator first:區間的開始
InputIterator last:區間的結束
Function fn:自定義動作,可以是函數指針或者仿函數對象、lambda表達式
作用:對[first,last)區間內的每個元素執行fn
返回值:返回每個元素執行的函數fn
實現:
template<classInputIterator,classFunction>Function for_each(InputIterator first, InputIterator last, Function fn){while(first!=last){fn (*first);++first;}return fn;// or, since C++11: return move(fn);}
// includes algorithm example#include<iostream>// std::cout#include<algorithm>// std::includes, std::sortbool myfunction (int i,int j){return i<j;}int main (){int container[]={5,10,15,20,25,30,35,40,45,50};int continent[]={40,30,20,10};std::sort (container,container+10);std::sort (continent,continent+4);// using default comparison:if( std::includes(container,container+10,continent,continent+4))std::cout <<"container includes continent!\n";// using myfunction as comp:if( std::includes(container,container+10,continent,continent+4, myfunction))std::cout <<"container includes continent!\n";return0;}Output:
container includes continent!
container includes continent!
// is_heap example#include<iostream>// std::cout#include<algorithm>// std::is_heap, std::make_heap, std::pop_heap#include<vector>// std::vectorint main (){std::vector<int> foo {9,5,2,6,4,1,3,8,7};if(!std::is_heap(foo.begin(),foo.end()))std::make_heap(foo.begin(),foo.end());std::cout <<"Popping out elements:";while(!foo.empty()){std::pop_heap(foo.begin(),foo.end());// moves largest element to backstd::cout <<' '<< foo.back();// prints backfoo.pop_back();// pops element out of container}std::cout <<'\n';return0;}Output:
Popping out elements:987654321
#include<iostream>// std::cout#include<algorithm>// std::is_heap_until, std::sort, std::reverse#include<vector>// std::vectorint main (){std::vector<int> foo {2,6,9,3,8,4,5,1,7};std::sort(foo.begin(),foo.end());std::reverse(foo.begin(),foo.end());auto last = std::is_heap_until (foo.begin(),foo.end());std::cout <<"The "<<(last - foo.begin())<<" first elements are a valid heap:";for(auto it=foo.begin(); it!=last;++it)std::cout <<' '<<*it;std::cout <<'\n';return0;}Most implementations consider a range sorted in reverse order a valid heap:
Possible output:
The 9 first elements are a valid heap:987654321
10.std::is_partitioned
原型:
template<classInputIterator,classUnaryPredicate>bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);