• <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)  編輯 收藏 引用 所屬分類: 泛型編程

            久久精品成人欧美大片| 一本一道久久a久久精品综合| 欧美激情一区二区久久久| 亚洲狠狠婷婷综合久久蜜芽| 国内精品人妻无码久久久影院| 久久综合狠狠色综合伊人| 久久人人爽人人爽人人片AV麻豆| 中文字幕乱码人妻无码久久| 欧洲性大片xxxxx久久久| 99久久精品国内| 77777亚洲午夜久久多人| AV狠狠色丁香婷婷综合久久| 久久精品国产99久久久香蕉| 精品国产乱码久久久久久郑州公司| 久久精品国产亚洲沈樵| 91精品国产高清久久久久久io| 狠狠人妻久久久久久综合| 国产一区二区三区久久| 伊人久久大香线蕉AV一区二区| 9久久9久久精品| 熟妇人妻久久中文字幕| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 久久天天躁狠狠躁夜夜2020老熟妇| 人妻精品久久无码区| 蜜臀久久99精品久久久久久小说| 久久免费国产精品| 国产精品日韩欧美久久综合| 久久天天躁狠狠躁夜夜2020| 青青青国产精品国产精品久久久久| 亚洲AV日韩精品久久久久| 无码八A片人妻少妇久久| 午夜精品久久久久久| 亚洲国产综合久久天堂| 亚洲精品成人网久久久久久| 人妻系列无码专区久久五月天| 久久99精品免费一区二区| 久久久久国产精品麻豆AR影院 | 72种姿势欧美久久久久大黄蕉| 伊人久久无码中文字幕| 亚洲αv久久久噜噜噜噜噜| 色综合久久久久综合体桃花网|