• <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)  編輯 收藏 引用

            国产99久久精品一区二区| 国内精品久久久久久久97牛牛| 久久精品中文字幕第23页| 久久影院午夜理论片无码| 91麻豆国产精品91久久久| 国产成年无码久久久免费| 久久久久亚洲AV片无码下载蜜桃 | 精产国品久久一二三产区区别| 久久精品国产久精国产果冻传媒| 亚洲AV日韩AV永久无码久久| 色综合久久久久无码专区| 99久久成人18免费网站| 成人综合久久精品色婷婷| 99久久精品午夜一区二区| 久久国产乱子伦精品免费午夜| 欧美日韩精品久久免费| 久久99精品久久久久久hb无码| 久久午夜综合久久| 久久久无码精品亚洲日韩按摩| 国产精品美女久久久网AV| 久久久久免费精品国产| 国产99久久久国产精品~~牛| 亚洲伊人久久成综合人影院 | 亚洲国产精品无码久久| 91精品国产色综久久| 久久久SS麻豆欧美国产日韩| 国产成人精品久久一区二区三区| 亚洲国产精品狼友中文久久久| 国产精品久久久久久久| 免费久久人人爽人人爽av| 国产成人99久久亚洲综合精品| 亚洲第一极品精品无码久久 | 久久久无码精品亚洲日韩软件| 久久久久亚洲av无码专区| 日韩精品无码久久一区二区三| 久久国产精品无码一区二区三区 | 国产精品99久久久久久董美香| 亚洲精品无码久久一线| 性欧美大战久久久久久久| 91久久香蕉国产熟女线看| 9久久9久久精品|