1. 程式人生 > >STL演算法之heap演算法,已排序區間演算法

STL演算法之heap演算法,已排序區間演算法

// 1. sort() 將區間元素以謂詞方式排序
// 2. stable_sort() sort()並保持區間元素的穩定性
// 3. partial_sort() 以謂詞找到前[beg,mid)
// 4. partial_sort_copy() 將區間1的排序結果複製到區間2
// 5. nth_element() 將區間前n個元素排序
// 6. make_heap() 將區間元素轉化成堆
// 7. push_heap() 加入新元素產生新堆
// 8. pop_heap() 將第一個元素移到尾部並將[beg,end-1)元素形成堆
// 9. sort_heap() 將堆進行排序(在原來排序準則下不再是堆)
// 10.binary_search() 判斷有序區間是否存在value
// 11.includes() 判斷有序區間是否是區間2子區間
// 12.lower_bound() 找出第一個大於等於value的元素
// upper_bound() 找出第一個大於value的元素
// equal_range() 找出等於value的範圍[first,last)
// 13.merge() 將兩個有序區間結合到另一序列
// 14.set_union() 找出兩個有序區間的並集複製到另一序列
// 15.set_intersection() 找出兩個有序區間的交集複製到另一序列
// 16.set_difference() 找出區間2對於區間1的差集並複製到另一序列
// 17.set_symmetric_difference() 找出區間2對於區間1的可重複差集並複製到另一序列
// 18.inplace_merge() 將[beg,end1) [end1,end] 結合

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// 1. sort() 將區間元素以謂詞方式排序
// 2. stable_sort() sort()並保持區間元素的穩定性
// 3. partial_sort() 以謂詞找到前[beg,mid)
// 4. partial_sort_copy() 將區間1的排序結果複製到區間2
// 5. nth_element() 將區間前n個元素排序
// 6. make_heap() 將區間元素轉化成堆
// 7. push_heap() 加入新元素產生新堆 // 8. pop_heap() 將第一個元素移到尾部並將[beg,end-1)元素形成堆 // 9. sort_heap() 將堆進行排序(在原來排序準則下不再是堆) // 10.binary_search() 判斷有序區間是否存在value // 11.includes() 判斷有序區間是否是區間2子區間 // 12.lower_bound() 找出第一個大於等於value的元素 // upper_bound() 找出第一個大於value的元素 // equal_range() 找出等於value的範圍[first,last) // 13.merge() 將兩個有序區間結合到另一序列
// 14.set_union() 找出兩個有序區間的並集複製到另一序列 // 15.set_intersection() 找出兩個有序區間的交集複製到另一序列 // 16.set_difference() 找出區間2對於區間1的差集並複製到另一序列 // 17.set_symmetric_difference() 找出區間2對於區間1的可重複差集並複製到另一序列 // 18.inplace_merge() 將[beg,end1) [end1,end] 結合 void test() { //1. vector<int> a{1,2,3,4,5,6,7,8,9 }; sort(a.begin(), a.end(), greater<>()); for (auto &v : a) cout << v << ends; cout << endl; vector<int> b{ 1,2,3,4,5,5,5,6,7,8 }; //2 stable_sort(b.begin(), b.end(), greater<>()); for (auto &v : b) cout << v << ends; cout << endl; //3 vector<int> c{ 2,2,3,4,5,6,5,4 }; partial_sort(c.begin(),c.begin() + 4,c.end(),greater<>()); for (auto &v : c) cout << v << ends; cout << endl; //4 vector<int> d(b.size()); partial_sort_copy(b.begin(), b.end(),d.begin(),d.end(),less<int>()); for (auto &v : d) cout << v << ends; cout << endl; vector<int> e{ 1,2,3,6,4,5,9,0 }; //5 nth_element(e.begin(), e.begin() + 4, e.end(), greater<>()); for (auto &v : e) cout << v << ends; cout << endl; //6 vector<int> f{ 1,5,8,9,0,2,3 }; make_heap(f.begin(), f.end(),greater<int>()); for (auto &v : f) cout << v << ends; //7 pop_heap(f.begin(), f.end(),greater<>()); cout << endl; f.pop_back(); for (auto &v : f) cout << v << ends; f.push_back(11); //8 push_heap(f.begin(), f.end(),greater<>()); for (auto &v : f) cout << v << ends; //9 sort_heap(f.begin(), f.end(),greater<>()); for (auto &v : f) cout << v << ends; cout << endl; if (is_heap(f.begin(), f.end(),greater<>())) cout << "f is delelop heap" << endl; else cout << "f is not develop heap" << endl; vector<int> g{1,2,3,4,5,6,7,8,9 }; //10 if (binary_search(g.begin(), g.end(), 5)) cout << "5 is exist in g" << endl; else cout << "5 is not exist in g" << endl; //11 vector<int> m{ 4,5,6}; if (includes(g.begin(), g.end(), m.begin(), m.end())) cout << "m includes in g" << endl; else cout << "m not includes in g" << endl; vector<int> n{ 1,2,3,4,5,6,7,8,9}; //12 auto fp = lower_bound(n.begin(), n.end(), 5); auto sp = upper_bound(n.begin(), n.end(), 5); cout << "n has how many 5 " << distance(fp, sp) << endl; auto ppos = equal_range(n.begin(), n.end(), 5); cout << "n has how many 5 " << distance(ppos.first, ppos.second) << endl; vector<int> j; //13 merge(m.begin(), m.end(), n.begin(), n.end(), back_inserter(j),less<>()); for (auto &v : j) cout << v << ends; cout << endl; //14 set_union(m.begin(), m.end(), n.begin(), n.end(), back_inserter(j)); for (auto &v : j) cout << v << ends; cout << endl; vector<int> i; //15 set_intersection(m.begin(), m.end(), n.begin(), n.end(), back_inserter(i)); for (auto &v : i) cout << v << ends; cout << endl; i.resize(0); //16 set_difference(n.begin(), n.end(), m.begin(), m.end(),back_inserter(i)); for (auto &v : i) cout << v << ends; i.resize(0); cout << endl; //17 set_symmetric_difference(m.begin(), m.end(), n.begin(), n.end(), back_inserter(i)); for (auto &v : i) cout << v << ends; cout << endl; i.resize(0); i.assign({ 1,2,3,4,5,6,7,8,1,2,3,4,5 }); auto pos = find(i.begin(), i.end(), 8); //18 inplace_merge(i.begin(), ++pos, i.end()); for (auto &v : i) cout << v << ends; } int main() { test(); system("pause"); return 0; }