目錄html
2、經常使用算法介紹linux
回到頂部ios
STL算法部分主要由頭文件<algorithm>,<numeric>,<functional>組成。要使用 STL中的算法函數必須包含頭文件 <algorithm>,對於數值算法須包含 <numeric>,<functional> 中則定義了一些模板類,用來聲明函數對象。算法
回到頂部數組
STL 中算法大體分爲四類:less
細緻分類可分爲 13 類,因爲算法過多,因此不一一作介紹,只選取幾個最經常使用的算法介紹。dom
查找算法共13個,包含在<algorithm>頭文件中,用來提供元素排序策略,這裏只列出一部分算法:函數
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; int main(int argc, char* argv[]) { int iarr[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 }; vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int)); /*** adjacent_find: 在iterator對標識元素範圍內,查找一對相鄰重複元素 ***/ // 原型: _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last) cout << "adjacent_find: "; cout << *adjacent_find(iv.begin(), iv.end()) << endl; /*** count: 利用等於操做符,把標誌範圍內的元素與輸入值比較,返回相等元素個數。 ***/ // 原型: count(_InIt _First, _InIt _Last, const _Ty& _Val) cout << "count(==7): "; cout << count(iv.begin(), iv.end(), 6) << endl;// 統計6的個數 /*** count_if: 利用輸入的操做符,對標誌範圍內的元素進行操做,返回結果爲true的個數。 ***/ // 原型: count_if(_InIt _First, _InIt _Last, _Pr _Pred) // 統計小於7的元素的個數 :9個 cout << "count_if(<7): "; cout << count_if(iv.begin(), iv.end(), bind2nd(less<int>(), 7)) << endl; /*** binary_search: 在有序序列中查找value,找到返回true。 ***/ // 原型: bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) cout << "binary_search: "; cout << binary_search(iv.begin(), iv.end(), 4) << endl; // 找到返回true /*** equal_range: 功能相似equal,返回一對iterator,第一個表示lower_bound,第二個表示upper_bound。 ***/ // 原型: equal_range(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) pair<vector<int>::iterator, vector<int>::iterator> pairIte; pairIte = equal_range(iv.begin(), iv.end(), 3); cout << "pairIte.first:" << *(pairIte.first) << endl;// lowerbound 3 cout << "pairIte.second:" << *(pairIte.second) << endl; // upperbound 4 /*** find: 利用底層元素的等於操做符,對指定範圍內的元素與輸入值進行比較。 ***/ // 原型: _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val) cout << "find: "; cout << *find(iv.begin(), iv.end(), 4) << endl; // 返回元素爲4的元素的下標位置 /*** find_if: 使用輸入的函數代替等於操做符執行find。 ***/ // 原型: _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred) cout << "find_if: " << *find_if(iv.begin(), iv.end(), bind2nd(greater<int>(), 2)) << endl; // 返回大於2的第一個元素的位置:3 /*** search: 給出兩個範圍,返回一個ForwardIterator,查找成功指向第一個範圍內第一次出現子序列的位置。 ***/ // 原型: _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2) // 在iv中查找 子序列 2 3 第一次出現的位置的元素 int iarr3[3] = { 2, 3 }; vector<int> iv3(iarr3, iarr3 + 2); cout << "search: " << *search(iv.begin(), iv.end(), iv3.begin(), iv3.end()) << endl; /*** search_n: 在指定範圍內查找val出現n次的子序列。 ***/ // 原型: _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1, _Diff2 _Count, const _Ty& _Val) // 在iv中查找 2個6 出現的第一個位置的元素 cout << "search_n: " << *search_n(iv.begin(), iv.end(), 2, 6) << endl; return 0; } /* adjacent_find: 6 count(==7): 3 count_if(<7): 9 binary_search: 1 pairIte.first:3 pairIte.second:4 find: 4 find_if: 3 search: 2 search_n: 6 */
排序算法共14個,包含在<algorithm>頭文件中,用來判斷容器中是否包含某個值,這裏只列出一部分算法:spa
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> // 定義了greater<int>() using namespace std; // 要注意的技巧 template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; // 若是想從大到小排序,能夠採用先排序後反轉的方式,也能夠採用下面方法: // 自定義從大到小的比較器,用來改變排序方式 bool Comp(const int& a, const int& b) { return a > b; } int main(int argc, char* argv[]) { int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 }; vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int)); vector<int> iv2(iarr1 + 4, iarr1 + 8); // 4 5 6 6 vector<int> iv3(15); /*** merge: 合併兩個有序序列,存放到另外一個序列 ***/ // iv1和iv2合併到iv3中(合併後會自動排序) merge(iv1.begin(), iv1.end(), iv2.begin(), iv2.end(), iv3.begin()); cout << "merge合併後: "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** random_shuffle: 對指定範圍內的元素隨機調整次序。 ***/ int iarr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; vector<int> iv4(iarr2, iarr2 + sizeof(iarr2) / sizeof(int)); // 打亂順序 random_shuffle(iv4.begin(), iv4.end()); cout << "random_shuffle打亂後: "; for_each(iv4.begin(), iv4.end(), display<int>()); cout << endl; /*** nth_element: 將範圍內的序列從新排序。 ***/ // 將小於iv.begin+5的放到左邊 nth_element(iv4.begin(), iv4.begin() + 5, iv4.end()); cout << "nth_element從新排序後: "; for_each(iv4.begin(), iv4.end(), display<int>()); cout << endl; /*** reverse: 將指定範圍內元素從新反序排序。 ***/ reverse(iv4.begin(), iv4.begin()); cout << "reverse翻轉後: "; for_each(iv4.begin(), iv4.end(), display<int>()); cout << endl; /*** sort: 以升序從新排列指定範圍內的元素。 ***/ // sort(iv4.begin(), iv4.end(), Comp); // 也可使用自定義Comp()函數 sort(iv4.begin(), iv4.end(), greater<int>()); cout << "sort排序(倒序): "; for_each(iv4.begin(), iv4.end(), display<int>()); cout << endl; /*** stable_sort: 與sort相似,不過保留相等元素之間的順序關係。 ***/ int iarr3[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6 }; vector<int> iv5(iarr3, iarr3 + sizeof(iarr3) / sizeof(int)); stable_sort(iv5.begin(), iv5.end(), greater<int>()); cout << "stable_sort排序(倒序): "; for_each(iv5.begin(), iv5.end(), display<int>()); cout << endl; return 0; } /* merge合併後: 0 1 2 3 4 4 5 5 6 6 6 6 6 7 8 random_shuffle打亂後: 8 1 6 2 0 5 7 3 4 nth_element從新排序後: 0 1 2 3 4 5 6 7 8 reverse翻轉後: 0 1 2 3 4 5 6 7 8 sort排序(倒序): 8 7 6 5 4 3 2 1 0 stable_sort排序(倒序): 6 5 4 4 3 3 2 1 0 */
刪除和替換算法共15個,包含在<numeric>頭文件中,這裏只列出一部分算法:指針
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> // 定義了greater<int>() using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; int main(int argc, char* argv[]) { int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int)); vector<int> iv2(9); /*** copy: 複製序列 ***/ // 原型: _OutIt copy(_InIt _First, _InIt _Last,_OutIt _Dest) copy(iv1.begin(), iv1.end(), iv2.begin()); cout << "copy(iv2): "; for_each(iv2.begin(), iv2.end(), display<int>()); cout << endl; /*** copy_backward: 與copy相同,不過元素是以相反順序被拷貝。 ***/ // 原型: _BidIt2 copy_backward(_BidIt1 _First, _BidIt1 _Last,_BidIt2 _Dest) copy_backward(iv1.begin(), iv1.end(), iv2.rend()); cout << "copy_backward(iv2): "; for_each(iv2.begin(), iv2.end(), display<int>()); cout << endl; /*** remove: 刪除指定範圍內全部等於指定元素的元素。 ***/ // 原型: _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) remove(iv1.begin(), iv1.end(), 5); // 刪除元素5 cout << "remove(iv1): "; for_each(iv1.begin(), iv1.end(), display<int>()); cout << endl; /*** remove_copy: 將全部不匹配元素複製到一個制定容器,返回OutputIterator指向被拷貝的末元素的下一個位置。 ***/ // 原型: _OutIt remove_copy(_InIt _First, _InIt _Last,_OutIt _Dest, const _Ty& _Val) vector<int> iv3(8); remove_copy(iv1.begin(), iv1.end(), iv3.begin(), 4); // 去除4 而後將一個容器的元素複製到另外一個容器 cout << "remove_copy(iv3): "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** remove_if: 刪除指定範圍內輸入操做結果爲true的全部元素。 ***/ // 原型: _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred) remove_if(iv3.begin(), iv3.end(), bind2nd(less<int>(), 6)); // 將小於6的元素 "刪除" cout << "remove_if(iv3): "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** remove_copy_if: 將全部不匹配元素拷貝到一個指定容器。 ***/ // 原型: _OutIt remove_copy_if(_InIt _First, _InIt _Last,_OutIt _Dest, _Pr _Pred) // 將iv1中小於6的元素 "刪除"後,剩下的元素再複製給iv3 remove_copy_if(iv1.begin(), iv1.end(), iv2.begin(), bind2nd(less<int>(), 4)); cout << "remove_if(iv2): "; for_each(iv2.begin(), iv2.end(), display<int>()); cout << endl; return 0; } /* copy(iv2): 0 1 2 3 4 5 6 7 8 copy_backward(iv2): 8 7 6 5 4 3 2 1 0 remove(iv1): 0 1 2 3 4 6 7 8 8 remove_copy(iv3): 0 1 2 3 6 7 8 8 remove_if(iv3): 6 7 8 8 6 7 8 8 remove_if(iv2): 4 6 7 8 8 3 2 1 0 */
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> // 定義了greater<int>() using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; int main(int argc, char* argv[]) { int iarr[] = { 8, 10, 7, 8, 6, 6, 7, 8, 6, 7, 8 }; vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int)); /*** replace: 將指定範圍內全部等於vold的元素都用vnew代替。 ***/ // 原型: void replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval) // 將容器中6 替換爲 3 replace(iv.begin(), iv.end(), 6, 3); cout << "replace(iv): "; for_each(iv.begin(), iv.end(), display<int>()); // 因爲_X是static 因此接着 增加 cout << endl; // iv:8 10 7 8 3 3 7 8 3 7 8 /*** replace_copy: 與replace相似,不過將結果寫入另外一個容器。 ***/ // 原型: _OutIt replace_copy(_InIt _First, _InIt _Last, _OutIt _Dest, const _Ty& _Oldval, const _Ty& _Newval) vector<int> iv2(12); // 將容器中3 替換爲 5,並將結果寫入另外一個容器。 replace_copy(iv.begin(), iv.end(), iv2.begin(), 3, 5); cout << "replace_copy(iv2): "; for_each(iv2.begin(), iv2.end(), display<int>()); cout << endl; // iv2:8 10 7 8 5 5 7 8 5 7 8 0(最後y一個殘留元素) /*** replace_if: 將指定範圍內全部操做結果爲true的元素用新值代替。 ***/ // 原型: void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val) // 將容器中小於 5 替換爲 2 replace_if(iv.begin(), iv.end(), bind2nd(less<int>(), 5), 2); cout << "replace_copy(iv): "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; // iv:8 10 7 8 2 5 7 8 2 7 8 /*** replace_copy_if: 與replace_if,不過將結果寫入另外一個容器。 ***/ // 原型: _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, const _Ty& _Val) // 將容器中小於 5 替換爲 2,並將結果寫入另外一個容器。 replace_copy_if(iv.begin(), iv.end(), iv2.begin(), bind2nd(equal_to<int>(), 8), 9); cout << "replace_copy_if(iv2): "; for_each(iv2.begin(), iv2.end(), display<int>()); cout << endl; // iv2:9 10 7 8 2 5 7 9 2 7 8 0(最後一個殘留元素) int iarr3[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, }; vector<int> iv3(iarr3, iarr3 + sizeof(iarr3) / sizeof(int)); int iarr4[] = { 8, 10, 7, 8, 6, 6, 7, 8, 6, }; vector<int> iv4(iarr4, iarr4 + sizeof(iarr4) / sizeof(int)); /*** swap: 交換存儲在兩個對象中的值。 ***/ // 原型: _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, const _Ty& _Val) // 將兩個容器中的第一個元素交換 swap(*iv3.begin(), *iv4.begin()); cout << "swap(iv3): "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** swap_range: 將指定範圍內的元素與另外一個序列元素值進行交換。 ***/ // 原型: _FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _Dest) // 將兩個容器中的所有元素進行交換 swap_ranges(iv4.begin(), iv4.end(), iv3.begin()); cout << "swap_range(iv3): "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** unique: 清除序列中相鄰的重複元素,和remove相似,它也不能真正刪除元素。 ***/ // 原型: _FwdIt unique(_FwdIt _First, _FwdIt _Last, _Pr _Pred) unique(iv3.begin(), iv3.end()); cout << "unique(iv3): "; for_each(iv3.begin(), iv3.end(), display<int>()); cout << endl; /*** unique_copy: 與unique相似,不過把結果輸出到另外一個容器。 ***/ // 原型: _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred) unique_copy(iv3.begin(), iv3.end(), iv4.begin()); cout << "unique_copy(iv4): "; for_each(iv4.begin(), iv4.end(), display<int>()); cout << endl; return 0; } /* replace(iv): 8 10 7 8 3 3 7 8 3 7 8 replace_copy(iv2): 8 10 7 8 5 5 7 8 5 7 8 0 replace_copy(iv): 8 10 7 8 2 2 7 8 2 7 8 replace_copy_if(iv2): 9 10 7 9 2 2 7 9 2 7 9 0 swap(iv3): 8 1 2 3 4 5 6 7 8 swap_range(iv3): 0 10 7 8 6 6 7 8 6 unique(iv3): 0 10 7 8 6 7 8 6 6 unique_copy(iv4): 0 10 7 8 6 7 8 6 8 */
排列組合算法共2個,包含在<algorithm>頭文件中,用來提供計算給定集合按必定順序的全部可能排列組合,這裏所有列出:
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; int main(int argc, char* argv[]) { int iarr[] = { 12, 17, 20, 22, 23, 30, 33, 40 }; vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int)); /*** next_permutation: 取出當前範圍內的排列,並從新排序爲下一個字典序排列。***/ // 原型: bool next_permutation(_BidIt _First, _BidIt _Last) // 生成下一個排列組合(字典序) next_permutation(iv.begin(), iv.end()); for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** prev_permutation: 取出指定範圍內的序列並將它從新排序爲上一個字典序排列。 ***/ // 原型: bool prev_permutation(_BidIt _First, _BidIt _Last) prev_permutation(iv.begin(), iv.end()); for_each(iv.begin(), iv.end(), display<int>()); cout << endl; return 0; } /* 12 17 20 22 23 30 40 33 12 17 20 22 23 30 33 40 */
數值算法共4個,包含在<numeric>頭文件中,分別是:
#include "stdafx.h" #include <iostream> #include <vector> #include <numeric> // 數值算法 #include <iterator> // 定義了ostream_iterator using namespace std; int main(int argc, char* argv[]) { int arr[] = { 1, 2, 3, 4, 5 }; vector<int> vec(arr, arr + 5); vector<int> vec2(arr, arr + 5); // accumulate: iterator對標識的序列段元素之和,加到一個由val指定的初始值上。 int temp; int val = 0; temp = accumulate(vec.begin(), vec.end(), val); cout << "accumulate(val = 0): " << temp << endl; val = 1; temp = accumulate(vec.begin(), vec.end(), val); cout << "accumulate(val = 1): " << temp << endl; // inner_product: 對兩個序列作內積(對應元素相乘,再求和)並將內積加到一個輸入的初始值上。 // 這裏是:1*1 + 2*2 + 3*3 + 4*4 + 5*5 val = 0; temp = inner_product(vec.begin(), vec.end(), vec2.begin(), val); cout << "inner_product(val = 0): " << temp << endl; // partial_sum: 建立一個新序列,其中每一個元素值表明指定範圍內該位置前全部元素之和。 // 第一次,1 第二次,1+2 第三次,1+2+3 第四次,1+2+3+4 ostream_iterator<int> oit(cout, " "); // 迭代器綁定到cout上做爲輸出使用 cout << "ostream_iterator: "; partial_sum(vec.begin(), vec.end(), oit);// 依次輸出前n個數的和 cout << endl; // 第一次,1 第二次,1-2 第三次,1-2-3 第四次,1-2-3-4 cout << "ostream_iterator(minus): "; partial_sum(vec.begin(), vec.end(), oit, minus<int>());// 依次輸出第一個數減去(除第一個數外到當前數的和) cout << endl; // adjacent_difference: 建立一個新序列,新序列中每一個新值表明當前元素與上一個元素的差。 // 第一次,1-0 第二次,2-1 第三次,3-2 第四次,4-3 cout << "adjacent_difference: "; adjacent_difference(vec.begin(), vec.end(), oit); // 輸出相鄰元素差值 後面-前面 cout << endl; // 第一次,1+0 第二次,2+1 第三次,3+2 第四次,4+3 cout << "adjacent_difference(plus): "; adjacent_difference(vec.begin(), vec.end(), oit, plus<int>()); // 輸出相鄰元素差值 後面-前面 cout << endl; return 0; } /* accumulate(val = 0): 15 accumulate(val = 1): 16 inner_product(val = 0): 55 ostream_iterator: 1 3 6 10 15 ostream_iterator(minus): 1 -1 -4 -8 -13 adjacent_difference: 1 1 1 1 1 adjacent_difference(plus): 1 3 5 7 9 */
生成和異變算法共6個,包含在<algorithm>頭文件中,這裏只列出一部分算法:
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; // 做用相似於上面結構體,只不過只能顯示int類型的數據 void printElem(int& elem) { cout << elem << " "; } template<class T> struct plus2 { void operator()(T&x)const { x += 2; } }; class even_by_two { private: static int _x; // 注意靜態變量 public: int operator()()const { return _x += 2; } }; int even_by_two::_x = 0; // 初始化靜態變量 int main(int argc, char* argv[]) { int iarr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int)); /*** fill: 將輸入值賦給標誌範圍內的全部元素。 ***/ // 原型: void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) fill(iv.begin(), iv.end(),5); cout << "fill: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** fill_n: 將輸入值賦給first到first+n範圍內的全部元素。 ***/ // 原型: _OutIt fill_n(_OutIt _Dest, _Diff _Count, const _Ty& _Val) fill_n(iv.begin(), 4, 3); // 賦4個3給iv cout << "fill_n: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** for_each: 用指定函數依次對指定範圍內全部元素進行迭代訪問,返回所指定的函數類型。 ***/ // 原型: _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func) for_each(iv.begin(), iv.end(), plus2<int>()); // 每一個元素+2 cout << "for_each: "; for_each(iv.begin(), iv.end(), printElem); // 輸出 cout << endl; /*** generate: 連續調用輸入的函數來填充指定的範圍。 ***/ // 原型: void generate(_FwdIt _First, _FwdIt _Last, _Fn0 _Func) // 使用even_by_two()函數返回的值,來填充容器iv generate(iv.begin(), iv.end(), even_by_two()); cout << "generate: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** generate_n: 與generate函數相似,填充從指定iterator開始的n個元素。 ***/ // 原型: _OutIt generate_n(_OutIt _Dest, _Diff _Count, _Fn0 _Func) // 使用even_by_two()函數返回的值,來填充容器iv的前三個值 generate_n(iv.begin(), 3, even_by_two()); cout << "generate_n: "; for_each(iv.begin(), iv.end(), display<int>()); // 因爲_X是static 因此接着 增加 cout << endl; /*** transform: 將輸入的操做做用與指定範圍內的每一個元素,併產生一個新的序列。 ***/ // 原型: _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) // 容器的全部值所有減2 transform(iv.begin(), iv.end(), iv.begin(), bind2nd(minus<int>(), 2)); cout << "transform: "; for_each(iv.begin(), iv.end(), display<int>()); // 因爲_X是static 因此接着 增加 cout << endl; return 0; } /* fill: 5 5 5 5 5 5 5 5 5 fill_n: 3 3 3 3 5 5 5 5 5 for_each: 5 5 5 5 7 7 7 7 7 generate: 2 4 6 8 10 12 14 16 18 generate_n: 20 22 24 8 10 12 14 16 18 transform: 18 20 22 6 8 10 12 14 16 */
關係算法共8個,包含在<algorithm>頭文件中,這裏只列出一部分算法:
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { int iarr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int> iv1(iarr, iarr + 5); vector<int> iv2(iarr, iarr + 9); // equal: 若是兩個序列在標誌範圍內元素都相等,返回true。 cout <<"equal: " << equal(iv1.begin(), iv1.end(), iv2.begin()) << endl;// 1 表示相等,由於只比較跟 iv1長度大小的數組 // includes: 判斷第一個指定範圍內的全部元素是否都被第二個範圍包含,使用底層元素的<操做符,成功返回true。 // 判斷判斷iv2中元素是否都出如今iv1中 cout << "includes: " << includes(iv1.begin(), iv1.end(), iv2.begin(), iv2.end()) << endl; // max: 返回兩個元素中較大一個。 cout << "max: " << max(iv1[0],iv1[1]) << endl; // min: 返回兩個元素中較小一個。 cout << "min: " << min(iv1[0], iv1[1]) << endl; // max_element: 返回一個ForwardIterator,指出序列中最大的元素。 cout << "max_element: " << *max_element(iv1.begin(), iv1.end()) << endl; // min_element: 返回一個ForwardIterator,指出序列中最小的元素。 cout << "min_element: " << *min_element(iv1.begin(), iv1.end()) << endl; // mismatch: 並行比較兩個序列,指出第一個不匹配的位置,返回一對iterator,標誌第一個不匹配元素位置。若是都匹配,返回每一個容器的last。 pair<vector<int>::iterator, vector<int>::iterator> pa; pa = mismatch(iv1.begin(), iv1.end(), iv2.begin()); if (pa.first == iv1.end()) // true 表示相等,由於只比較跟iv1長度大小的數組 cout << "第一個向量與第二個向量匹配" << endl; else { cout << "兩個向量不一樣點--第一個向量點:" << *(pa.first) << endl; // 這樣寫很危險,應該判斷是否到達end cout << "兩個向量不一樣點--第二個向量點:" << *(pa.second) << endl; } return 0; } /* equal: 1 includes: 0 max: 2 min: 1 max_element: 5 min_element: 1 第一個向量與第二個向量匹配 */
集合算法共4個,包含在<algorithm>頭文件中,這裏所有列出:
#include "stdafx.h" #include <iostream> #include <set> #include <algorithm> #include <iterator> using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; int main(int argc, char* argv[]) { int iarr1[] = { 1, 3, 5, 7, 9, 11 }; int iarr2[] = { 1, 1, 2, 3, 5, 8, 13 }; multiset<int> s1(iarr1, iarr1 + 6); multiset<int> s2(iarr2, iarr2 + 7); cout << "s1: "; for_each(s1.begin(), s1.end(), display<int>()); cout << endl; cout << "s2: "; for_each(s2.begin(), s2.end(), display<int>()); cout << endl; /*** set_union: 構造一個有序序列,包含兩個序列中全部的不重複元素。 ***/ // 原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) cout << "union of s1 and s2: "; // 兩個集合合併,相同元素個數取 max(m,n)。 set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " ")); cout << endl; /*** set_intersection: 構造一個有序序列,其中元素在兩個序列中都存在。 ***/ // 原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) cout << "Intersection of s1 and s2: "; // 兩個集合交集,相同元素個數取 min(m,n). set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " ")); cout << endl; /*** set_difference: 構造一個有序序列,該序列僅保留第一個序列中存在的而第二個中不存在的元素。 ***/ // 原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) cout << "Intersection of s1 and s2: "; // 兩個集合差集 就是去掉S1中 的s2 set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " ")); cout << endl; /*** set_symmetric_difference: 構造一個有序序列,該序列取兩個序列的對稱差集(並集-交集)。 ***/ // 原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) cout << "Intersection of s1 and s2: "; // 兩個集合對稱差集:就是取兩個集合互相沒有的元素 。兩個排序區間,元素相等指針後移,不等輸出小的並前進 // 相同元素的個數 abs(m-n) set_symmetric_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; } /* s1: 1 3 5 7 9 11 s2: 1 1 2 3 5 8 13 union of s1 and s2: 1 1 2 3 5 7 8 9 11 13 Intersection of s1 and s2: 1 3 5 Intersection of s1 and s2: 7 9 11 Intersection of s1 and s2: 1 2 7 8 9 11 13 */
集合算法共4個,包含在<algorithm>頭文件中,這裏只列出一部分算法:
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> struct display { void operator()(const T&x) const { cout << x << " "; } }; int main(int argc, char* argv[]) { int iarr[] = { 4, 5, 1, 3, 2 }; vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int)); /*** make_heap: 把指定範圍內的元素生成一個堆。 ***/ // 原型: void make_heap(_RanIt _First, _RanIt _Last) make_heap(iv.begin(), iv.end()); cout << "make_heap: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** pop_heap: 並不真正把最大元素從堆中彈出,而是從新排序堆。 ***/ // 原型: void pop_heap(_RanIt _First, _RanIt _Last) pop_heap(iv.begin(), iv.end()); iv.pop_back(); cout << "pop_heap: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; /*** push_heap: 假設first到last-1是一個有效堆,要被加入到堆的元素存放在位置last-1,從新生成堆。 ***/ // 原型: void push_heap(_RanIt _First, _RanIt _Last) iv.push_back(6); push_heap(iv.begin(), iv.end()); cout << "push_heap: "; for_each(iv.begin(), iv.end(), display<int>()); cout << endl; return 0; } /* make_heap: 5 4 1 3 2 pop_heap: 4 3 1 2 push_heap: 6 4 1 2 3 */