• <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>

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            STL算法(Algorithms):堆(heap)

            1、make_heap:使序列變成堆
            原型:
            template <class RandomAccessIterator>
              void make_heap ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>
              void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
                               Compare comp );
            范例:
             1// range heap example
             2#include <iostream>
             3#include <algorithm>
             4#include <vector>
             5using namespace std;
             6
             7int main () {
             8  int myints[] = {10,20,30,5,15};
             9  vector<int> v(myints,myints+5);
            10
            11  make_heap (v.begin(),v.end());
            12  cout << "initial max heap   : " << v.front() << endl;
            13
            14  pop_heap (v.begin(),v.end()); v.pop_back();
            15  cout << "max heap after pop : " << v.front() << endl;
            16
            17  v.push_back(99); push_heap (v.begin(),v.end());
            18  cout << "max heap after push: " << v.front() << endl;
            19
            20  sort_heap (v.begin(),v.end());
            21
            22  cout << "final sorted range :";
            23  for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
            24
            25  cout << endl;
            26
            27  return 0;
            28}

            29


            2、push_heap:壓棧(入棧)
            原型:
            template <class RandomAccessIterator>
              void push_heap ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>
              void push_heap ( RandomAccessIterator first, RandomAccessIterator last,
                               Compare comp );
            3、pop_heap彈棧(出棧)
            原型:
            template <class RandomAccessIterator>
              void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>
              void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,
                               Compare comp );
            范例:
             1// range heap example
             2#include <iostream>
             3#include <algorithm>
             4#include <vector>
             5using namespace std;
             6
             7int main () {
             8  int myints[] = {10,20,30,5,15};
             9  vector<int> v(myints,myints+5);
            10  vector<int>::iterator it;
            11
            12  make_heap (v.begin(),v.end());
            13  cout << "initial max heap   : " << v.front() << endl;
            14
            15  pop_heap (v.begin(),v.end()); v.pop_back();
            16  cout << "max heap after pop : " << v.front() << endl;
            17
            18  v.push_back(99); push_heap (v.begin(),v.end());
            19  cout << "max heap after push: " << v.front() << endl;
            20
            21  sort_heap (v.begin(),v.end());
            22
            23  cout << "final sorted range :";
            24  for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
            25
            26  cout << endl;
            27
            28  return 0;
            29}

            30

            4、sort_heap對堆排序
            原型:template <class RandomAccessIterator>
              void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>
              void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,
                               Compare comp );
            范例:
             1// range heap example
             2#include <iostream>
             3#include <algorithm>
             4#include <vector>
             5using namespace std;
             6
             7int main () {
             8  int myints[] = {10,20,30,5,15};
             9  vector<int> v(myints,myints+5);
            10  vector<int>::iterator it;
            11
            12  make_heap (v.begin(),v.end());
            13  cout << "initial max heap   : " << v.front() << endl;
            14
            15  pop_heap (v.begin(),v.end()); v.pop_back();
            16  cout << "max heap after pop : " << v.front() << endl;
            17
            18  v.push_back(99); push_heap (v.begin(),v.end());
            19  cout << "max heap after push: " << v.front() << endl;
            20
            21  sort_heap (v.begin(),v.end());
            22
            23  cout << "final sorted range :";
            24  for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
            25
            26  cout << endl;
            27
            28  return 0;
            29}

            30

            注:例子來源于www.cplusplus.com網站

            posted on 2012-01-12 13:53 Benjamin 閱讀(953) 評論(0)  編輯 收藏 引用 所屬分類: 泛型編程

            99精品久久精品一区二区| 久久久久久久99精品免费观看| 色偷偷888欧美精品久久久| 国产精品成人久久久久久久| 亚洲国产天堂久久综合| 91久久成人免费| 日韩欧美亚洲国产精品字幕久久久 | 亚洲精品成人久久久| 亚洲精品乱码久久久久久自慰| 国产精品久久久久久久久鸭| 久久国产精品视频| 久久久av波多野一区二区| 午夜不卡888久久| 久久久久久人妻无码| 亚洲精品tv久久久久久久久久| 一本色道久久88加勒比—综合| 亚洲香蕉网久久综合影视| 人妻无码精品久久亚瑟影视| 久久香蕉一级毛片| 久久久无码人妻精品无码| 亚洲精品视频久久久| 精品久久久久久无码人妻热| 99热成人精品热久久669| 99久久国产综合精品女同图片| 欧美国产精品久久高清| 久久久久久国产a免费观看不卡| 国产精品久久免费| 国产精品美女久久久久网| 亚洲精品无码久久千人斩| 久久这里只有精品首页| 一级做a爰片久久毛片看看| 久久久久人妻一区精品| 大蕉久久伊人中文字幕| 久久香蕉综合色一综合色88| AV无码久久久久不卡网站下载| 2021久久国自产拍精品| 韩国无遮挡三级久久| 久久青草国产手机看片福利盒子| 天天久久狠狠色综合| 亚洲欧美精品伊人久久| 国产日韩久久久精品影院首页|