heap implementation in c++ code example
Example 1: min heap in c++
priority_queue <int, vector<int>, greater<int>> minHeap;
Example 2: how to allocate on heap in c++
#include <iostream>
#include <string>
using String = std::string;
class Entity
{
private:
String m_Name;
public:
Entity() : m_Name("Unknown") {}
Entity(const String& name) : m_Name(name) {}
const String& GetName() const {
return m_Name;
};
};
int main() {
int* b = new int;
int* c = new int[50];
Entity* e1 = new Entity;
Entity* e = new Entity[50];
Entity* alloc = (Entity*)malloc(sizeof(Entity));
delete e;
Entity* e3 = new(c) Entity();
}
Example 3: Priority Queue using Min Heap in c++
#include <bits/stdc++.h>
using namespace std;
int main ()
{
priority_queue <int> pq;
pq.push(5);
pq.push(1);
pq.push(10);
pq.push(30);
pq.push(20);
while (pq.empty() == false)
{
cout << pq.top() << " ";
pq.pop();
}
return 0;
}