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

            靜以修身,儉以養(yǎng)德,非澹薄無(wú)以明志,非寧?kù)o無(wú)以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評(píng)論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            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對(duì)堆排序
            原型: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

            注:例子來(lái)源于www.cplusplus.com網(wǎng)站

            posted on 2012-01-12 13:53 Benjamin 閱讀(945) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 泛型編程

            九九99精品久久久久久| A狠狠久久蜜臀婷色中文网| 久久久精品一区二区三区| 久久精品国产福利国产秒| 爱做久久久久久| 久久精品卫校国产小美女| 久久亚洲中文字幕精品有坂深雪| 欧美日韩中文字幕久久伊人| 欧美成a人片免费看久久| 久久精品人人做人人爽97| 久久99精品久久久久久噜噜| 精品久久亚洲中文无码| 亚洲午夜久久久精品影院| 国产成人精品综合久久久久| 欧美综合天天夜夜久久| 伊人久久综合无码成人网| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 狠狠精品久久久无码中文字幕| 久久亚洲国产最新网站| 国产99久久久国产精免费| 国产综合久久久久| 五月丁香综合激情六月久久| 日本精品一区二区久久久| 国产伊人久久| 国产午夜福利精品久久| 成人妇女免费播放久久久| 日日噜噜夜夜狠狠久久丁香五月 | 国内精品人妻无码久久久影院| 午夜精品久久久久久影视777| 热久久国产精品| 久久久精品一区二区三区| 精品熟女少妇av免费久久| 亚洲AV无码久久精品蜜桃| 久久人人添人人爽添人人片牛牛| 久久久久亚洲精品天堂| 久久久久久久久久久| 国产日韩久久免费影院| 国产精品久久久久9999| 久久久国产乱子伦精品作者| 色婷婷综合久久久久中文一区二区 | 精品免费久久久久久久|