// is_permutation example#include<iostream>// std::cout#include<algorithm>// std::is_permutation#include<array>// std::arrayint main (){std::array<int,5> foo ={1,2,3,4,5};std::array<int,5> bar ={3,1,4,5,2};if( std::is_permutation (foo.begin(), foo.end(), bar.begin()))std::cout <<"foo and bar contain the same elements.\n";return0;}Output:
foo and bar contain the same elements.
// is_sorted_until example#include<iostream>// std::cout#include<algorithm>// std::is_sorted_until, std::prev_permutation#include<array>// std::arrayint main (){std::array<int,4> foo {2,4,1,3};std::array<int,4>::iterator it;do{// try a new permutation:std::prev_permutation(foo.begin(),foo.end());// print range:std::cout <<"foo:";for(int& x:foo) std::cout <<' '<< x;it=std::is_sorted_until(foo.begin(),foo.end());std::cout <<" ("<<(it-foo.begin())<<" elements sorted)\n";}while(it!=foo.end());std::cout <<"the range is sorted!\n";return0;}Output:
foo:2341(3 elements sorted)
foo:2314(2 elements sorted)
foo:2143(1 elements sorted)
foo:2134(1 elements sorted)
foo:1432(2 elements sorted)
foo:1423(2 elements sorted)
foo:1342(3 elements sorted)
foo:1324(2 elements sorted)
foo:1243(3 elements sorted)
foo:1234(4 elements sorted)
the range is sorted!
4.std::iter_swap
原型:
template<classForwardIterator1,classForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b);
參數:
ForwardIterator1 a:區間的開始
ForwardIterator1 b:區間的結束
作用:交換a,b兩個迭代器的元素內容
返回值:沒有返回值
實現:
template<classForwardIterator1,classForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b){swap (*a,*b);}
// range heap example#include<iostream>// std::cout#include<algorithm>// std::make_heap, std::pop_heap, std::push_heap, std::sort_heap#include<vector>// std::vectorint main (){int myints[]={10,20,30,5,15};std::vector<int>v(myints,myints+5);std::make_heap (v.begin(),v.end());std::cout <<"initial max heap : "<< v.front()<<'\n';std::pop_heap (v.begin(),v.end()); v.pop_back();std::cout <<"max heap after pop : "<< v.front()<<'\n';v.push_back(99); std::push_heap (v.begin(),v.end());std::cout <<"max heap after push: "<< v.front()<<'\n';std::sort_heap (v.begin(),v.end());std::cout <<"final sorted range :";for(unsigned i=0; i<v.size(); i++)std::cout <<' '<< v[i];std::cout <<'\n';return0;}Edit & RunOutput:
initial max heap :30
max heap after pop :20
max heap after push:99
final sorted range :510152099
template<classForwardIterator>ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ){if(first==last)return last;ForwardIterator largest = first;while(++first!=last)if(*largest<*first)// or: if (comp(*largest,*first)) for version (2)largest=first;return largest;}
案例:
// min_element/max_element example#include<iostream>// std::cout#include<algorithm>// std::min_element, std::max_elementboolmyfn(int i,int j){return i<j;}struct myclass {booloperator()(int i,int j){return i<j;}} myobj;int main (){int myints[]={3,7,2,5,6,4,9};// using default comparison:std::cout <<"The smallest element is "<<*std::min_element(myints,myints+7)<<'\n';std::cout <<"The largest element is "<<*std::max_element(myints,myints+7)<<'\n';// using function myfn as comp:std::cout <<"The smallest element is "<<*std::min_element(myints,myints+7,myfn)<<'\n';std::cout <<"The largest element is "<<*std::max_element(myints,myints+7,myfn)<<'\n';// using object myobj as comp:std::cout <<"The smallest element is "<<*std::min_element(myints,myints+7,myobj)<<'\n';std::cout <<"The largest element is "<<*std::max_element(myints,myints+7,myobj)<<'\n';return0;}Edit & RunOutput:
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9