共用方式為


heap

說明如何使用堆積 Visual C++ 標準樣板程式庫 (STL) 函式。

template<class RandomAccessIterator> inline
   void make_heap(
      RandomAccessIterator First,
      RandomAccessIterator Last
   )
template<class RandomAccessIterator> inline
   void sort_heap(
      RandomAccessIterator First,
      RandomAccessIterator Last
   )
template<class RandomAccessIterator> inline
   void push_heap(
      RandomAccessIterator First,
      RandomAccessIterator Last
   )
template<class RandomAccessIterator> inline
   void pop_heap(
      RandomAccessIterator First,
      RandomAccessIterator Last
   )

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

堆集是一系列的組織,例如二進位樹狀目錄中的項目。堆積中的每個元素會對應至樹狀節點。在序列中的第一個值 [First...Last) 是根目錄,而是在堆積中的最大值。堆積中的每個項目滿足下列: 每個項目小於或等於其父系。最大的項目會儲存在根目錄中,按住所有子系變得較小的值。Make_heap 函式將轉換的範圍 [First...Last) 堆積。Sort_heap 函式排序以建立一系列make_heap函式。Push_heap 函式的堆積中插入新的值。Pop_heap 函式交換所指定的堆積中的第一個和最後一個項目 [First, Last),然後再降低然後再還原堆積屬性的其中一個序列的長度。堆積的 nonpredicate 版本的函式使用運算子 < 進行比較。

範例

// heapfunc.cpp
// compile with: /EHsc
// 
// Functions:
//    make_heap : convert a sequence to a heap
//    sort_heap : sort a heap
//    push_heap : insert an element in a heap
//    pop_heap  : remove the top element from a heap

// disable warning C4786: symbol greater than 255 characters,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std;

int main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of int
    typedef vector<int > IntVector ;

    //Define an iterator for template class vector of strings
    typedef IntVector::iterator IntVectorIt ;

    IntVector Numbers(VECTOR_SIZE) ;

    IntVectorIt it ;

    // Initialize vector Numbers
    Numbers[0] = 4 ;
    Numbers[1] = 10;
    Numbers[2] = 70 ;
    Numbers[3] = 10 ;
    Numbers[4] = 30 ;
    Numbers[5] = 69 ;
    Numbers[6] = 96 ;
    Numbers[7] = 100;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = Numbers.begin(); it != Numbers.end(); it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // convert Numbers into a heap
    make_heap(Numbers.begin(), Numbers.end()) ;

    cout << "After calling make_heap\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = Numbers.begin(); it != Numbers.end(); it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // sort the heapified sequence Numbers
    sort_heap(Numbers.begin(), Numbers.end()) ;

    cout << "After calling sort_heap\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = Numbers.begin(); it != Numbers.end(); it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    //insert an element in the heap
    Numbers.push_back(7) ;
    push_heap(Numbers.begin(), Numbers.end()) ;

    // you need to call make_heap to re-assert the
    // heap property
    make_heap(Numbers.begin(), Numbers.end()) ;

    cout << "After calling push_heap and make_heap\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = Numbers.begin(); it != Numbers.end(); it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // remove the root element from the heap Numbers
    pop_heap(Numbers.begin(), Numbers.end()) ;

    cout << "After calling pop_heap\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = Numbers.begin(); it != Numbers.end(); it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
}

Output

Numbers { 4 10 70 10 30 69 96 100  }

After calling make_heap

Numbers { 100 30 96 10 4 69 70 10  }

After calling sort_heap

Numbers { 4 10 10 30 69 70 96 100  }

After calling push_heap and make_heap

Numbers { 100 69 96 30 4 70 10 10 7  }

After calling pop_heap

Numbers { 96 69 70 30 4 7 10 10 100  }

需求

標頭: <algorithm>

請參閱

概念

標準樣板程式庫範例