Predicate Versions of heap
在 Visual C++ 演示如何使用 堆 STL 功能的特性版本。
template<class RandomAccessIterator, class Compare> inline
void make_heap(
RandomAccessIterator First,
RandomAccessIterator Last,
Compare Compare
)
template<class RandomAccessIterator, class Compare> inline
void sort_heap(
RandomAccessIterator First,
RandomAccessIterator Last,
Compare Compare
)
template<class RandomAccessIterator, class Compare> inline
void push_heap(
RandomAccessIterator First,
RandomAccessIterator Last,
Compare Compare
)
template<class RandomAccessIterator, class Compare> inline
void pop_heap(
RandomAccessIterator First,
RandomAccessIterator Last,
Compare Compare
)
备注
说明 |
---|
类/参数名在原型不匹配版本在头文件。修改某些提高可读性。 |
堆是元素顺序组织方式与二叉树。每个堆元素都对应于树节点。在序列 [First的第一个值。Last) 是根和谓词所排序。例如,因此,如果谓词会更大,在堆中的每个元素满足以下语句:每个元素大于或等于其父级。最小元素在根存储区,因此,所有子级表示进度较大的值。make_heap 功能将范围 [First。Last) 到堆。sort_heap 功能排序使用 make_heap 功能,创建的序列。push_heap 功能插入一个新值赋给堆。pop_heap 功能交换堆中的第一个和最后一个元素指定的 [First, Last),然后减少序列的长度按一个在回收堆属性。堆函数的谓词版本用于比较比较函数。
示例
// heap_PVfunctions.cpp
// compile with: /EHsc
// Illustrates how to use the predicate versions
// of the make_heap, sort_heap, push_heap
// and pop_heap functions.
//
// 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 character,
// 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(), greater<int>()) ;
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(), greater<int>()) ;
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 ;
make_heap(Numbers.begin(), Numbers.end(), greater<int>()) ;
//insert an element in the heap
Numbers.push_back(7) ;
push_heap(Numbers.begin(), Numbers.end(), greater<int>()) ;
cout << "After calling push_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(), greater<int>()) ;
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 ;
}
要求
标题: algorithm