template <class RandomAccessIterator> void push_heap( RandomAccessIterator xFirst, RandomAccessIterator xLast );
#include <algorithm>
template <class RandomAccessIterator, class BinaryPred> void push_heap( RandomAccessIterator xFirst, RandomAccessIterator xLast, BinaryPred xComp );
#include <iostream> #include <vector> #include <algorithm> int main() { using namespace std; // Create a vector instance vector<int> qV; qV.push_back(45); qV.push_back(83); qV.push_back(3); qV.push_back(49); qV.push_back(97); qV.push_back(16); qV.push_back(34); // Make the vector a heap make_heap(qV.begin(), qV.end()); // Add an extra element to the end of the heap qV.push_back(51); vector<int>::iterator qIter; // Output the heap and last element cout << "Output heap with last : "; for (qIter = qV.begin(); qIter != qV.end(); ++qIter) { cout << *qIter << " "; } cout << endl; // push the last int (51) into the heap push_heap(qV.begin(), qV.end()); // Output the new vector cout << "Output the entire heap: "; for (qIter = qV.begin(); qIter != qV.end(); ++qIter) { cout << *qIter << " "; } cout << endl; // Keep the window open cin.get(); return 0; }
© 20072024 XoaX.net LLC. All rights reserved.