一,巡防算法
for_each(容器起始地址,容器結束地址,要執行的方法)
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- template<class T>
- struct plus2
- {
- void operator()(T&x)const
- {
- x+=2;
- }
- };
- void printElem(int& elem)
- {
- cout << elem << endl;
- }
- int main()
- {
- int ia[]={0,1,2,3,4,5,6};
- for_each(ia,ia+7,printElem);//輸出
- int ib[]={7,8,9,10,11,12,13};
- vector<int> iv(ib,ib+7);
- for_each(iv.begin(),iv.end(),plus2<int>());//更改元素
- for_each(iv.begin(),iv.end(),printElem);//輸出
- return 0;
- }
int *find(int *begin,int *end,int value)
前閉後合的區間 begin,end中,查找value如果查找到了就返回第一個符合條件的元素,否則返回end指針
- #include <iostream>
- #include <algorithm>
- using namespace std;
- void printElem(int& elem)
- {
- cout << elem << endl;
- }
- int main()
- {
- int ia[]={0,1,2,3,4,5,6};
- int *i= find(ia,ia+7,9);//在整個數組中查找元素 9
- int *j= find(ia,ia+7,3);//在整個數組中查找元素 3
- int *end=ia+7;//數組最後位置
- if(i == end)
- cout<<"沒有找到元素 9"<<endl;
- else
- cout<<"找到元素9"<<endl;
- if(j == end)
- cout<<"沒有找到元素 3"<<endl;
- else
- cout<<"找到元素"<<*j<<endl;
- return 0;
- }
包含在<numeric>頭文件中
- #include <iostream>
- #include <numeric> //數值算法
- #include <vector>
- #include <functional>
- #include <iterator>
- #include <math.h>
- using namespace std;
- int main()
- {
- int ia[]={1,2,3,4,5};
- vector<int> iv(ia,ia+5);
- cout<<accumulate(iv.begin(),iv.end(),0)<<endl; //累加 初值爲0
- cout<<accumulate(iv.begin(),iv.end(),0,minus<int>())<<endl; //累加 符號位負
- cout<<inner_product(iv.begin(),iv.end(),iv.begin(),10)<<endl;//兩個數組內積 初值爲10
- cout<<inner_product(iv.begin(),iv.end(),iv.begin(),10,minus<int>(),plus<int>())<<endl;//10-(1+1)-(2+2)
- ostream_iterator<int> oite(cout," ");//迭代器綁定到cout上作爲輸出使用
- partial_sum(iv.begin(),iv.end(),oite);//依次輸出前n個數的和
- cout<<endl;
- partial_sum(iv.begin(),iv.end(),oite,minus<int>());//依次輸出第一個數減去(除第一個數外到當前數的和)
- cout<<endl;
- adjacent_difference(iv.begin(),iv.end(),oite); //輸出相鄰元素差值 前面-後面
- cout<<endl;
- adjacent_difference(iv.begin(),iv.end(),oite,plus<int>()); //輸出相鄰元素差值 前面+後面 。前面更改影響後面元素
- cout<<endl;
- cout<<pow(10,3)<<endl; // 平方
- /* VC 不支持 只有安裝了才SGI STL支持
- int n=3;
- iota(iv.begin(),iv.end(),n);//在指定區間填入n n+1 n+2
- for(int i=0;i<iv.size();++i)
- cout<<iv[i]<<" ";
- */
- return 0;
- }
四,基本算法
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- template<typename T>
- struct display
- {
- void operator()(const T &x)const
- {
- cout<<x<<" ";
- }
- };
- int main()
- {
- int ia[]={0,1,2,3,4,5,6,7,8};
- vector<int> iv1(ia,ia+5);
- vector<int> iv2(ia,ia+9);
- pair<vector<int>::iterator,vector<int>::iterator> pa;
- pa=mismatch(iv1.begin(),iv1.end(),iv2.begin());
- cout<<"兩個數組不同點--第一個數組點:"<<*(pa.first)<<endl; //這樣寫很危險,應該判斷是否到達end
- cout<<"兩個數組不同點--第二個數組點:"<<*(pa.second)<<endl;
- //更改之後
- if(pa.first == iv1.end())
- cout<<"第一個數組與第二個數組匹配"<<endl;
- cout<<equal(iv1.begin(),iv1.end(),iv2.begin())<<endl;// 1 表示 相等,因爲只比較跟 iv1長度大小的數組
- cout<<equal(iv1.begin(),iv1.end(),&ia[3])<<endl;// 0 表示 不相等
- cout<<equal(iv1.begin(),iv1.end(),&ia[3],less<int>())<<endl;// 1 表示 前者小於後者
- fill(iv1.begin(),iv1.end(),9);//將iv1區間內填滿 9
- for_each(iv1.begin(),iv1.end(),display<int>());
- cout<<endl;
- fill_n(iv1.begin(),3,6);//從iv1區間開始填 3個6
- for_each(iv1.begin(),iv1.end(),display<int>());
- cout<<endl;
- vector<int>::iterator ite1=iv1.begin();
- vector<int>::iterator ite2=ite1;
- advance(ite2,3);//向前跳3個
- iter_swap(ite1,ite2);//交換迭代器指向的元素
- for_each(iv1.begin(),iv1.end(),display<int>());
- cout<<"\nmax:"<<max(*ite1,*ite2)<<endl;
- cout<<"min:"<<min(*ite1,*ite2)<<endl;
- swap(*ite1,*ite2);
- for_each(iv1.begin(),iv1.end(),display<int>());
- cout<<endl;
- string stra1[]={"a","b","c"};
- string stra2[]={"d","e","f"};
- cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2)<<endl;//按照字典序 前者小於後者
- cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2,greater<string>())<<endl;//按照字典序 前者不大於後者
- return 0;
- }
五,copy()對不同容器複製;關於輸出區間與輸入區間重疊的討論
- #include <iostream>
- #include <algorithm>
- #include <deque>
- using namespace std;
- template<class T>
- struct display
- {
- void operator()(const T &x)const
- {
- cout<<x<<" ";
- }
- };
- int main()
- {
- //以下複製區間沒有問題
- int ia1[]={0,1,2,3,4,5,6,7,8};
- copy(ia1+2,ia1+7,ia1);//將下標2-6複製給 1-5
- for_each(ia1,ia1+9,display<int>()); //2,3,4,5,6,5,6,7,8
- cout<<endl;
- //輸出區間的起點與輸入區間重疊,可能會有問題。但本例copy採用memmove()執行實際複製操作
- int ia2[]={0,1,2,3,4,5,6,7,8};
- copy(ia2+2,ia2+7,ia2+4);//將下標2-6複製給 4-8
- for_each(ia2,ia2+9,display<int>()); //0,1,2,3,2,3,4,5,6
- cout<<endl;
- //以下複製區間沒有問題
- int ia3[]={0,1,2,3,4,5,6,7,8};
- deque<int> id(ia3,ia3+9);
- deque<int>::iterator first=id.begin();
- deque<int>::iterator last=id.end();
- deque<int>::iterator result=id.begin();
- ++++first;
- cout<<*first<<endl;
- ----last;
- cout<<*last<<endl;
- cout<<*result<<endl;
- copy(first,last,result);
- for_each(id.begin(),id.end(),display<int>());//2,3,4,5,6,5,6,7,8
- cout<<endl;
- //以下複製區間存在問題,由於實際複製沒有采用memove(),結果錯誤
- int ia4[]={0,1,2,3,4,5,6,7,8};
- deque<int> ide(ia4,ia4+9);
- deque<int>::iterator first1=ide.begin();
- deque<int>::iterator last1=ide.end();
- deque<int>::iterator result1=ide.begin();
- advance(result1,4);//注意這裏跟上面不一樣
- ++++first1;
- cout<<*first1<<endl;
- ----last1;
- cout<<*last1<<endl;
- cout<<*result1<<endl;
- copy(first1,last1,result1);
- for_each(ide.begin(),ide.end(),display<int>());// 0,1,2,3,2,3,2,3,2不是預期的 0,1,2,3,2,3,4,5,6
- cout<<endl;
- return 0;
- }
【注意】如果以vector 容器替代deque容器則每種情況都正確,因爲vector迭代器其實是個源生指針,調用的copy()算法以mommove()執行實際複製。
copy_backward(first,last,result); //逆向複製,將迭代器first - last位置的元素逆向複製到 從result-1開始的逆向區間
原型:void *memmove( void * dest, const void * src, size_t count );
用法:#include <string.h>或#include <memory.h> 功能:由src所指內存區域複製count個字節到dest所指內存區域。 說明:src和dest所指內存區域可以重疊,但複製後dest內容會被更改。函數返回指向dest的指針。採取先拷貝再複製的方式,有效解決了dest和src區域重疊問題 相關函數:memset、memcpy、strcpy 參考博文http://blog.csdn.net/tianshuai11/article/details/7624419
實例
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char s[]="Golden Global View";
- memmove(s,s+7,strlen(s)+1-7);
- printf("%s",s);
- return 0;
- }
六,Set方法
- #include <iostream>
- #include <set>
- #include <algorithm>
- #include <iterator>
- using namespace std;
- template <class T>
- struct display
- {
- void operator()(const T &x)
- {
- cout<<x<<" ";
- }
- };
- int main()
- {
- int ia1[]={1,3,5,7,9,11};
- int ia2[]={1,1,2,3,5,8,13};
- multiset<int> s1(ia1,ia1+6);
- multiset<int> s2(ia2,ia2+7);
- for_each(s1.begin(),s1.end(),display<int>());
- cout<<endl;
- for_each(s2.begin(),s2.end(),display<int>());
- cout<<endl;
- multiset<int>::iterator first1 = s1.begin();
- multiset<int>::iterator last1 = s1.end();
- multiset<int>::iterator first2 = s2.begin();
- multiset<int>::iterator last2 = s2.end();
- cout<<"union of s1 and s2: ";
- //兩個集合合併,相同元素個數取 max(m,n)。
- set_union(first1,last1,first2,last2,ostream_iterator<int>(cout," "));
- cout<<endl;
- first1=s1.begin();
- first2=s2.begin();
- cout<<"Intersection of s1 and s2: ";
- //兩個集合交集,相同元素個數取 min(m,n).
- set_intersection(first1,last1,first2,last2,ostream_iterator<int>(cout," "));
- cout<<endl;
- first1=s1.begin();
- first2=s2.begin();
- cout<<"Intersection of s1 and s2: ";
- //兩個集合差集 就是去掉S1中 的s2
- set_difference(first1,last1,first2,last2,ostream_iterator<int>(cout," "));
- cout<<endl;
- first1=s1.begin();
- first2=s2.begin();
- cout<<"Intersection of s1 and s2: ";
- //兩個集合對稱差集:就是取兩個集合互相沒有的元素 。兩個排序區間,元素相等指針後移,不等輸出小的並前進
- //相同元素的個數 abs(m-n)
- set_symmetric_difference(first1,last1,first2,last2,ostream_iterator<int>(cout," "));
- cout<<endl;
- return 0;
- }
七,其他算法(運算邏輯相對單純的算法)
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <functional>
- #include <vector>
- using namespace std;
- template <class T>
- struct display
- {
- void operator()(const T &x)const
- {
- cout<<x<<" ";
- }
- };
- struct even
- {
- bool operator()(int x)const
- {
- return x%2?false:true;
- }
- };
- class even_by_two
- {
- private:
- static int _x; //注意靜態變量
- public:
- int operator()()const
- {
- return _x+=2;
- }
- };
- int even_by_two::_x=0;
- int main()
- {
- int ia[]={0,1,2,3,4,5,6,6,6,7,8};
- vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));
- //找出iv之中相鄰元素值相等的第一個元素
- cout<<*adjacent_find(iv.begin(),iv.end())<<endl;
- cout<<*adjacent_find(iv.begin(),iv.end(),equal_to<int>())<<endl; //仿函數
- cout<<count(iv.begin(),iv.end(),6)<<endl;//統計6的個數
- cout<<count_if(iv.begin(),iv.end(),bind2nd(less<int>(),7))<<endl;//統計小於7的元素的個數 :9個
- cout<<*find(iv.begin(),iv.end(),4)<<endl; //返回元素爲4的元素的下標位置
- cout<<*find_if(iv.begin(),iv.end(),bind2nd(greater<int>(),2))<<endl; //返回大於2的第一個元素的位置:3
- vector<int> iv2(ia+6,ia+8);//6 6
- for(int i=0;i<iv2.size();++i)
- cout<<iv2[i]<<" ";
- cout<<endl;
- //返回iv序列中 iv2序列 出現的最後一個位置(再往後三個位置的值):8
- cout<<"find_end:"<<*(find_end(iv.begin(),iv.end(),iv2.begin(),iv2.end())+3)<<endl;
- //返回iv序列中 iv2序列 出現的最後一個位置(再往後三個位置的值):7
- cout<<"find_first_of:"<<*(find_first_of(iv.begin(),iv.end(),iv2.begin(),iv2.end())+3)<<endl;
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //遍歷整個iv2區間並執行 even_by_two操作
- generate(iv2.begin(),iv2.end(),even_by_two());
- for_each(iv2.begin(),iv2.end(),display<int>());
- cout<<endl;
- //遍歷區間(給出起點和長度),對每個遍歷元素執行even_by_two操作
- generate_n(iv.begin(),3,even_by_two());
- for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl;
- //刪除元素6 尾端可能有殘餘數據
- remove(iv.begin(),iv.end(),6);
- for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //8 10 3 4 5 7 8 6 6 7 8 (最後四個是殘留數據)
- //去除value 然後將一個容器的元素複製到另一個 容器。仍然可能有殘留元素
- vector<int> iv3(12);//重新申請空間
- remove_copy(iv.begin(),iv.end(),iv3.begin(),6);
- for_each(iv3.begin(),iv3.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //8 10 3 4 5 7 8 7 8 0 0 (最後兩個是殘留元素)
- //將小於6的元素 "刪除" iv 此時爲 8 10 3 4 5 7 8 6 6 7 8
- remove_if(iv.begin(),iv.end(),bind2nd(less<int>,6));
- for_each(iv1.begin(),iv1.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //8 10 7 8 6 6 7 8 6 7 8 (最後三個是殘留元素)
- //將小於7的元素 "刪除" iv3元素:8 10 3 4 5 7 8 7 8 0 0 (最後兩個是殘留元素)
- remove_copy_if(iv.begin(),iv.end(),iv3.begin(),bind2nd(less<int>,7));
- for_each(iv3.begin(),iv3.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //8 10 7 8 7 8 7 8 8 0 0(最後三個殘留元素)
- return 0;
- }
第二段算法示例:
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <functional>
- using namespace std;
- template <class T>
- struct display
- {
- void operator()(const T &x)const
- {
- cout<<x<<" ";
- }
- };
- int main()
- {
- int ia[]={8,10,7,8,6,6,7,8,6,7,8};
- vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));
- //將容器中6 替換爲 3
- replace(iv.begin(),iv.end(),6,3);
- for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //iv:8 10 7 8 3 3 7 8 3 7 8
- vector<int> iv2(12);
- //將容器中3 替換爲 5 放入另一個容器
- replace_copy(iv.begin(),iv.end(),iv2.begin(),3,5);
- for_each(iv2.begin(),iv2.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //iv2:8 10 7 8 5 5 7 8 5 7 8 0(最後y一個殘留元素)
- //將容器中小於 5 替換爲 2
- replace_if(iv.begin(),iv.end(),bind2nd(less<int>(),5),2);
- for_each(iv.begin(),iv.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //iv:8 10 7 8 2 5 7 8 2 7 8
- //將容器中小於 5 替換爲 2
- replace_copy_if(iv.begin(),iv.end(),iv2.begin(),bind2nd(equal_to<int>(),8),9);
- for_each(iv2.begin(),iv2.end(),display<int>()); //由於_X是static 所以接着 增長
- cout<<endl; //iv2:9 10 7 8 2 5 7 9 2 7 8 0(最後一個殘留元素)
- //逆向重排每一個元素 (倒置)
- reverse(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl; //iv:8 7 2 8 7 5 2 8 7 10 8
- //逆向重排每一個元素 (倒置)
- reverse_copy(iv.begin(),iv.end(),iv2.begin());
- for_each(iv2.begin(),iv2.end(),display<int>());
- cout<<endl; //iv2:8 10 7 8 2 5 7 8 2 7 8 0 (最後一個殘留元素)
- // 互換元素 [bigin,middle) [middle,end)
- rotate(iv.begin(),iv.begin()+4,iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;//iv:7 2 2 8 7 10 8 8 7 2 8
- // 互換元素 [bigin,middle) [middle,end)
- rotate_copy(iv.begin(),iv.begin()+5,iv.end(),iv2.begin());
- for_each(iv2.begin(),iv2.end(),display<int>());
- cout<<endl;//iv2:10 8 8 7 2 8 7 2 2 8 7 0 (最後一個是殘留元素)
- //在iv中查找 子序列 2 8 第一次出現的位置的元素
- int ia2[3]={2,8};
- vector<int> iv3(ia2,ia2+2);
- cout<<*search(iv.begin(),iv.end(),iv3.begin(),iv3.end())<<endl; //2
- //在iv中查找 2個8 出現的第一個位置的元素
- cout<<*search_n(iv.begin(),iv.end(),2,8)<<endl; //8
- //在iv中查找 3個小於8 出現的第一個位置的元素
- cout<<*search_n(iv.begin(),iv.end(),3,8,less<int>())<<endl; //7
- swap_ranges(iv3.begin(),iv3.end(),iv.begin());
- cout<<"iv:";
- for_each(iv.begin(),iv.end(),display<int>());//iv:2 8 2 8 7 10 8 8 7 2 8
- cout<<endl;
- cout<<"iv3:";
- for_each(iv3.begin(),iv3.end(),display<int>()); //iv3: 7 2
- cout<<endl;
- //全部減2
- transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(),2));
- for_each(iv.begin(),iv.end(),display<int>());//0 6 0 6 5 8 6 6 5 0 6
- cout<<endl;
- //兩個區間元素相加然後放到 iv上
- transform(iv.begin(),iv.end(),iv.begin(),iv.begin(),plus<int>());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl; //0 12 0 12 10 16 12 12 10 0 12
- return 0;
- }
第三段算法示例:
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <functional>
- using namespace std;
- template <class T>
- struct display
- {
- void operator()(const T &x)const
- {
- cout<<x<<" ";
- }
- };
- struct even
- {
- bool operator()(int x)const
- {
- return x%2?false:true;
- }
- };
- int main()
- {
- int ia[]={0,1,2,3,4,5,6,6,6,7,8};
- vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));
- vector<int> iv2(ia+4,ia+8);//4 5 6 6
- vector<int> iv3(15);
- cout<<*max_element(iv.begin(),iv.end())<<endl;
- cout<<*min_element(iv.begin(),iv.end())<<endl;
- //判斷iv2中元素是否都出現在 iv中
- cout<<includes(iv.begin(),iv.end(),iv2.begin(),iv2.end())<<endl;
- //iv 和iv2合併到iv3中
- merge(iv.begin(),iv.end(),iv2.begin(),iv2.end(),iv3.begin());
- for_each(iv3.begin(),iv3.end(),display<int>());
- cout<<endl;
- //符合條件的 放到前面,不符合條件的放到後面
- partition(iv3.begin(),iv3.end(),even());
- for_each(iv3.begin(),iv3.end(),display<int>());
- cout<<endl;
- //去除連續並且重複的元素
- unique(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //去除連續並且重複的元素
- unique_copy(iv.begin(),iv.end(),iv3.begin());
- for_each(iv3.begin(),iv3.end(),display<int>());
- cout<<endl;
- return 0;
- }
八,複雜算法示例(解釋在源碼中)
- #include <iostream>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- struct even //是否是奇數
- {
- bool operator()(int x)const
- {
- return x%2?false:true;
- }
- };
- template<class T>
- struct display
- {
- void operator()(T &x)const
- {
- cout<<x<<" ";
- }
- };
- int main()
- {
- int ia[] = {12,17,20,22,23,30,33,40};
- vector<int> iv(ia,ia+sizeof(ia)/sizeof(int));
- //返回可以插入的第一個位置
- cout<<*lower_bound(iv.begin(),iv.end(),21)<<endl; //22
- cout<<*upper_bound(iv.begin(),iv.end(),21)<<endl; //22
- //返回可以插入的最後一個位置
- cout<<*lower_bound(iv.begin(),iv.end(),22)<<endl; //22
- cout<<*upper_bound(iv.begin(),iv.end(),22)<<endl; //23
- //二分查找某個元素,返回是否找到
- cout<<binary_search(iv.begin(),iv.end(),33)<<endl; //1
- cout<<binary_search(iv.begin(),iv.end(),34)<<endl; //0
- //生成下一個排列組合(字典序)
- next_permutation(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- prev_permutation(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //打亂順序
- random_shuffle(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //找出最小的4個元素 放在前四個 後面順序不一定有序
- partial_sort(iv.begin(),iv.begin()+4,iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //排序(缺省爲遞增排序)
- sort(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //排序(設置爲遞減)
- sort(iv.begin(),iv.end(),greater<int>());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- iv.push_back(22);
- iv.push_back(30);
- iv.push_back(17);
- //排序並保持原相對位置
- stable_sort(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;//12 17 17 20 22 22 23 30 30 33 40
- pair<vector<int>::iterator,vector<int>::iterator> pairIte;
- //返回等於22的一個小區間
- pairIte = equal_range(iv.begin(),iv.end(),22);
- cout<<*(pairIte.first)<<endl;//lowerbound 22
- cout<<*(pairIte.second)<<endl; //upperbound 23
- //這裏返回一個空區間
- pairIte = equal_range(iv.begin(),iv.end(),25);
- cout<<*(pairIte.first)<<endl;//lowerbound 30
- cout<<*(pairIte.second)<<endl; //upperbound 30
- //打亂順序
- random_shuffle(iv.begin(),iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //將小於iv.begin+5的放到左邊
- nth_element(iv.begin(),iv.begin()+5,iv.end());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //將小於iv.begin+5的放到右邊
- nth_element(iv.begin(),iv.begin()+5,iv.end(),greater<int>());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- //排序
- stable_sort(iv.begin(),iv.end(),even());
- for_each(iv.begin(),iv.end(),display<int>());
- cout<<endl;
- return 0;
- }