// 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);
// 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 back
std::cout <<' '<< foo.back();// prints back
foo.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);