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

            久久人爽人人爽人人片AV| 久久99毛片免费观看不卡| 亚洲国产日韩欧美久久| 久久这里只有精品视频99| 97精品国产97久久久久久免费| 一本色道久久综合狠狠躁| 国产精品久久久久久搜索| 久久久网中文字幕| 国产午夜免费高清久久影院| 成人国内精品久久久久影院VR| 亚洲精品高清一二区久久| 丰满少妇高潮惨叫久久久| 久久久久综合中文字幕| 久久精品麻豆日日躁夜夜躁| 精品久久久久久久中文字幕| 一本一本久久aa综合精品| 93精91精品国产综合久久香蕉| 久久人人爽人人爽人人片AV不 | 伊人色综合久久天天人守人婷| 色偷偷偷久久伊人大杳蕉| 久久精品成人免费观看97| 精品无码久久久久久午夜| 久久免费视频1| 久久精品中文字幕一区| 久久福利青草精品资源站免费 | 久久久久亚洲AV成人片| 欧美精品福利视频一区二区三区久久久精品 | 久久无码AV一区二区三区| 91精品国产高清久久久久久io| 久久精品国产亚洲综合色| 亚洲av日韩精品久久久久久a| 久久久久无码精品| 国产精品99久久久久久宅男| 国产午夜精品久久久久免费视| 中文字幕久久波多野结衣av| 一级女性全黄久久生活片免费| 久久久精品日本一区二区三区| 久久精品二区| 日韩十八禁一区二区久久| 久久精品成人影院| 久久夜色精品国产噜噜亚洲a|