• <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 閱讀(955) 評論(0)  編輯 收藏 引用 所屬分類: 泛型編程

            97久久国产露脸精品国产| 中文字幕久久欲求不满| 国内精品久久久久影院老司| 伊人久久大香线蕉综合5g| 亚洲欧美国产精品专区久久| 午夜精品久久久久久99热| 99久久精品国产免看国产一区| 国产精品一区二区久久| 久久亚洲av无码精品浪潮| 欧美噜噜久久久XXX| 久久久久久av无码免费看大片| 中文字幕日本人妻久久久免费 | 亚洲午夜无码AV毛片久久| 无码超乳爆乳中文字幕久久 | 97久久超碰国产精品旧版| 久久精品国产精品亚洲艾草网美妙| 久久综合九色综合网站| 久久精品国产色蜜蜜麻豆| 漂亮人妻被黑人久久精品| 亚洲欧洲久久av| 久久99精品国产麻豆婷婷| 精品综合久久久久久97超人| 狠狠色婷婷久久一区二区| 久久精品成人| 亚洲一区中文字幕久久| 99国产欧美久久久精品蜜芽| 久久AV高潮AV无码AV| 亚洲色欲久久久久综合网 | 亚洲伊人久久综合影院| 久久99热这里只有精品国产| 久久99国产亚洲高清观看首页| 亚洲女久久久噜噜噜熟女| 四虎国产精品成人免费久久| 香蕉久久影院| 国产精品久久久香蕉| 久久久无码精品亚洲日韩京东传媒| 亚洲欧美另类日本久久国产真实乱对白 | 久久精品国产久精国产果冻传媒 | 青青久久精品国产免费看| 久久综合一区二区无码| 亚洲七七久久精品中文国产|