I can suggest you that to carefully go through the following code of lines that can demonstrates you how to use the sort_heap() function in the C++ programming as follows :
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int mt[] = {10,20,30,5,15};
vector<int> z(mt,mt+5);
vector<int>::iterator x;
make_heap (z.begin(),z.end());
cout << "initial max heap : " << z.front() << endl;
pop_heap (z.begin(),z.end()); z.pop_back();
cout << "max heap after pop : " << z.front() << endl;
z.push_back(99); push_heap (z.begin(),z.end());
cout << "max heap after push: " << z.front() << endl;
sort_heap (z.begin(),z.end());
cout << "final sorted range :";
for (unsigned j=0; j<z.size(); j++) cout << " " << z[j];
cout << endl;
return 0;
}
Output:
Code:
initial max heap : 30
max heap after pop : 20
max heap after push: 99
final sorted range : 5 10 15 20 99
Bookmarks