• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life is Good.

            Enhance Tech and English
            隨筆 - 65, 文章 - 20, 評論 - 21, 引用 - 0
            數據加載中……

            STL 堆棧操作技術

            STL 中與heap 有關的操作有 如下幾個 :

                     

            make_heap(),   pop_heap(),  push_heap(),  sort_heap(),  is_heap(); 

             

            is_heap() :

             

            原型如下 :

            1. bool is_heap(iterator start, iterator end); 

            ->判斷迭代器[start, end] 區間類的元素是否構成一個堆.  是返回true ,否則返回 false.

            2. bool is_heap(iterator start, iterator end, StrictWeakOrdering cmp); 

            ->判斷迭代器[start, end] 區間類的元素在cmp條件下是否構成一個堆. 是返回true ,否則返回 false.

                     

            make_heap() :

             原型如下 :

            1. void make_heap( random_access_iterator start, random_access_iterator end );         

            2. void make_heap( random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp ); 

             -> 迭代器[start , end] 區間內的元素生成一個堆. 默認使用 元素類型  操作符 進行判斷堆的類型, 因此生成的是大頂堆 .

             ->當使用了 版本2, 系統使用 用戶定義的 cmp 函數來構建一個堆 

             ->值得注意的是,  make_heap 改變了 迭代器所指向的 容器 的值.

             pop_heap() :

            原型如下 :

            1.  void pop_heap( random_access_iterator start, random_access_iterator end );

            2.  void pop_heap( random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp );

            ->pop_heap() 并不是真的把最大(最?。┑脑貜亩阎袕棾鰜?/span>而是重新排序堆. 它把首元素末元素交換,然后將[first,last-1)的數據再做成一個堆。 

            此時, 原來的 首元素 位于迭代器 end-1 的位置,  它已不再屬于堆的一員!  

            ->如果使用了 版本2 , 在交換了 首元素末元素 ,使用 cmp 規則 重新構建一個堆.

             push_heap() :

             原型如下 :

            1.   void push_heap( random_access_iterator start, random_access_iterator end );

            2.   void push_heap( random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp ); 

             -> 算法假設迭代器區間[start, end-1)內的元素已經是一個有效堆, 然后把 end-1 迭代器所指元素加入堆. 

             -> 如果使用了 cmp 參數, 將使用 cmp 規則構建堆.        

            sort_heap() : 

            原型如下 :

             1. void sort_heap (random_access_iterator start, random_access_iterator end);

             2. void sort_heap (random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp); 

            -> 堆結構被完全破壞, 相當于對元素進行排序, 效果和排序算法類似.  

            -> 如果使用了 cmp 參數, 將使用 cmp 規則排序堆.

             示例代碼:

            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <algorithm>
            #include 
            <vector>
            #include 
            <functional> // for greater<>, less<>
            using namespace std;


            int _tmain(int argc, _TCHAR* argv[])
            {
              
            // v.begin and v.end can by replaced by &number[0],  &number[9]
              int number[10= {2,1,10,8,90,30,9,98,6,7};
              vector
            <int> v(number,number+10);

              
            //make_heap (v.begin(),v.end()); // the first one is the largest one, others not sure.
              
            //cout << "Initial Max heap: " <<v.front()<< endl;

              
            //make_heap (v.begin(),v.end(),less<int>()); // the first one is the largest one, others not sure.
              
            //cout << "Initial Max heap : " <<v.front()<< endl;

              make_heap (v.begin(),v.end(),greater
            <int>()); // the first one is the smallest one, others not sure.
              cout << "Initial Min heap: " <<v.front()<< endl;

              
            //不是真的把最大(最小)的元素從堆中彈出來。而是重新排序堆。它
              
            // 把first和last交換,然后將[first,last-1)的數據再做成一個堆。
              
            //pop_heap (v.begin(),v.end());
              
            //pop_heap (v.begin(),v.end(),less<int>());
              pop_heap (v.begin(),v.end(),greater<int>());

              v.pop_back(); 
            // Pop up the last one

              
            //cout << "Max heap after pop : " << v.front() << endl;
              cout << "Min heap after pop : " << v.front() << endl;

              v.push_back(
            80); // Put to the last one

              
            //push_heap (v.begin(),v.end()); // Keep the first one is the largest one
              
            //push_heap (v.begin(),v.end(), less<int>()); // Keep the first one is the largest one
              push_heap (v.begin(),v.end(),greater<int>()); // Keep the first one is the smallest one

              
            //cout << "max heap after push: " << v.front() << endl;
              cout << "Min heap after push: " << v.front() << endl;

              
            //sort_heap (v.begin(),v.end()); // sort by descent
              
            //sort_heap (v.begin(),v.end(), less<int>()); // sort by descent
              sort_heap (v.begin(),v.end(),greater<int>()); // sort by ascent

              cout 
            << "final sorted range :";
              
            for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];

              cout 
            << endl;

              
            return 0;
            }

            posted on 2011-03-08 22:04 Mike Song 閱讀(1003) 評論(0)  編輯 收藏 引用

            久久精品国产亚洲av麻豆图片| 亚洲人成网站999久久久综合| 精品久久久久久成人AV| 国产精品久久久天天影视| 99久久www免费人成精品| 亚洲精品视频久久久| 色偷偷88888欧美精品久久久 | 久久乐国产精品亚洲综合| 久久男人AV资源网站| 久久精品无码一区二区无码 | 久久黄色视频| 亚洲AV无码久久精品成人| 国产精品无码久久四虎| 天天躁日日躁狠狠久久| 久久久久国产精品麻豆AR影院| 久久亚洲精品人成综合网| 久久国产香蕉一区精品| 亚洲国产精品久久久久| 日韩精品久久久肉伦网站| 污污内射久久一区二区欧美日韩 | 99久久99久久| 久久综合给合久久狠狠狠97色| 四虎久久影院| 久久99精品久久久久久9蜜桃 | 久久免费看黄a级毛片| 欧美一区二区精品久久| 亚洲AV无码成人网站久久精品大| 香蕉99久久国产综合精品宅男自 | 伊人久久一区二区三区无码| 国产亚洲欧美精品久久久| 久久久久精品国产亚洲AV无码| 午夜精品久久久久久久无码| 久久久久久极精品久久久 | 99久久精品无码一区二区毛片| 久久久噜噜噜久久中文福利| 国产成人无码精品久久久性色| 人人狠狠综合久久亚洲| 性做久久久久久久久浪潮| 久久综合亚洲色一区二区三区| 久久久亚洲AV波多野结衣| 久久久久波多野结衣高潮|