Pop_heap prototype:
STD: pop_heap
| Default (1) |
template <class RandomAccessIterator> void pop_heap (RandomAccessIterator first, RandomAccessIterator last); |
| Custom (2) |
template <class RandomAccessIterator, class Compare> void pop_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp); |
This function moves the first element (the maximum value) in the heap to the position of last-1. Then, the remaining elements are re-constructed into a heap (including the value at the previous-1 position, which is moved to first). However, the maximum value of the heap is not deleted.
After this method is called, the elements in the range still maintain the attributes of the heap.
A simple example:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ vector<int> vi{1,7,5,6,8,9,3}; cout<<"at first vi="; for(int i:vi) cout<<i<<" "; cout<<endl; make_heap(vi.begin(),vi.end()); cout<<"after make_heap(vi.begin(),vi.end())\nvi="; for(int i:vi) cout<<i<<" "; cout<<endl; pop_heap(vi.begin(),vi.end());cout<<"after pop_heap(vi.begin(),vi.end())"<<endl; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; } Run:
As you can see, 9 remains at the last-1 position in the original range. If necessary, you need to manually delete it.
In addition, the preceding elements are still stored in heap format.
Push_heap prototype:
STD: push_heap
| Default (1) |
template <class RandomAccessIterator> void push_heap (RandomAccessIterator first, RandomAccessIterator last); |
| Custom (2) |
template <class RandomAccessIterator, class Compare> void push_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp); |
This function is called after an element is inserted into a heap to insert the element into the heap.
This sentence is a bit difficult.
It is easy to see an example.
// range heap example#include <iostream> // std::cout#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap#include <vector> // std::vectorint main () { int myints[] = {10,20,30,5,15}; std::vector<int> v(myints,myints+5); std::make_heap (v.begin(),v.end()); std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap (v.begin(),v.end()); v.pop_back(); std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(99); std::push_heap (v.begin(),v.end()); std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap (v.begin(),v.end()); std::cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << '\n'; return 0;}Run:
Take a closer look at this sentence:
v.push_back(99); std::push_heap (v.begin(),v.end());
Add the element to the end () Position of the original range, and then call the push_heap function!
That is to say, the elements to be inserted are first inserted at the end of the sequence and then inserted into the heap!
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: [email protected]
Yu gdut
------------------------------------------------------------------
STL algorithm pop_heap, push_heap (45)