max heap delete c++ code example
Example 1: max heap c++ stl;
// C++ program to show that priority_queue is by
// default a Max Heap
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main ()
{
// Creates a max heap
priority_queue <int> pq;
pq.push(5);
pq.push(1);
pq.push(10);
pq.push(30);
pq.push(20);
// One by one extract items from max heap
while (pq.empty() == false)
{
cout << pq.top() << " ";
pq.pop();
}
return 0;
}
Example 2: how to allocate on heap in c++
//placement new in c++
char *buf = new char[sizeof(string)]; // pre-allocated buffer
string *p = new (buf) string("hi"); // placement new
string *q = new string("hi"); // ordinary heap allocation
/*Standard C++ also supports placement new operator, which constructs
an object on a pre-allocated buffer. This is useful when building a
memory pool, a garbage collector or simply when performance and exception
safety are paramount (there's no danger of allocation failure since the memory
has already been allocated, and constructing an object on a pre-allocated
buffer takes less time):
*/