• <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() 并不是真的把最大(最小)的元素從堆中彈出來而是重新排序堆. 它把首元素末元素交換,然后將[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 閱讀(1006) 評論(0)  編輯 收藏 引用

            久久婷婷五月综合国产尤物app| 99久久亚洲综合精品成人| 人人狠狠综合久久亚洲| 久久影院午夜理论片无码| 性欧美大战久久久久久久| 久久久久精品国产亚洲AV无码 | 久久久久亚洲AV片无码下载蜜桃| 欧美熟妇另类久久久久久不卡| AV无码久久久久不卡蜜桃| 久久精品免费大片国产大片| 久久久久亚洲AV成人网人人网站 | 亚洲成色www久久网站夜月| 久久精品国产亚洲AV无码娇色| 精品国产热久久久福利| 久久天天躁狠狠躁夜夜avapp| 99久久99这里只有免费的精品| 亚洲国产精品综合久久一线| 国产91色综合久久免费分享| 国产精品久久久久免费a∨| 免费观看久久精彩视频| 日本久久久久亚洲中字幕| 久久综合视频网站| 国产精品一区二区久久精品无码| 久久精品国产亚洲AV香蕉| 久久精品国产一区二区| 精品久久久久久国产免费了| 久久亚洲精品国产精品| 2020久久精品亚洲热综合一本| 国内精品久久久久国产盗摄| 国产精品久久久久无码av| 久久精品人成免费| 久久夜色精品国产噜噜噜亚洲AV | 精品无码久久久久久国产| av午夜福利一片免费看久久| 久久综合国产乱子伦精品免费| 欧美黑人激情性久久| 亚洲精品乱码久久久久久久久久久久| 久久久久亚洲av成人无码电影| 精品熟女少妇aⅴ免费久久| 国产精品免费久久久久电影网| 91精品国产综合久久久久久|