新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++實(shí)現(xiàn)堆
#includeusing namespace std; #include #include //仿函數(shù) template struct Less { bool operator()(const T& left, const T& right) { return left < right; } }; template struct Greater { bool operator()(const T& left, const T& right) { return left > right; } }; template >//默認(rèn)為小堆 class Heap { public: Heap() {} Heap(const T* array, size_t size) { for (size_t i = 0; i < size; ++i) { _a.push_back(array[i]); } for (int i = (_a.size()-2)/2; i >= 0; --i) { _AdjustDown(i); } } void Push(const T& x) { _a.push_back(x); _AdjustUp(_a.size()-1); } void Pop() { assert(!_a.empty()); swap(_a[0], _a[_a.size()-1]); _a.pop_back(); _AdjustDown(0); } T& GetTop() { assert(!_a.empty()); return _a[0]; } bool Empty() { return _a.empty(); } size_t Size() { return _a.size(); } void Print() { for (size_t i = 0; i < _a.size(); ++i) { cout<<_a[i]<<" "; } cout< 0) { if (compare(_a[child], _a[parent])) { swap(_a[parent], _a[child]); child = parent; parent = (child-1)/2; } else { break; } } } protected: vector _a; }; void Test() { int a[10] = {10, 11, 13, 12, 16, 18, 15, 17, 14, 19}; Heap > hp1(a, sizeof(a)/sizeof(a[0])); hp1.Print(); cout<<"size:"<
10年積累的成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有加查免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
分享題目:C++實(shí)現(xiàn)堆
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/jgjcoc.html