Skip to main content
added 44 characters in body
Source Link

I am fairly new to C++ and I was trying to implement a Min Heap using Priority Queue STL in C++. I looked around the web and chanced upon a code, the link to which I have added at the bottom.

I understand how vectors work and I also understand what a functor is but I can't understand how 'return i>j' plays a part in arranging the elements from minimum to maximum.

I will post the code here.

#include <queue> #include <iostream> using namespace std; struct comparator { bool operator()(int i, int j) { return i > j; } }; int main(int argc, char const *argv[]) { priority_queue<int, std::vector<int>, comparator> minHeap; minHeap.push(10); minHeap.push(5); minHeap.push(12); minHeap.push(3); minHeap.push(3); minHeap.push(4); while (!minHeap.empty()) { cout << minHeap.top() << " "; minHeap.pop(); } return 0; } 

Could someone please trace the program's execution? I have been trying to understand how this program works for the past couple of hours with no luck.

I got the code from here

I am fairly new to C++ and I was trying to implement a Min Heap using Priority Queue STL in C++. I looked around the web and chanced upon a code, the link to which I have added at the bottom. I understand what a functor is but I can't understand how 'return i>j' plays a part in arranging the elements from minimum to maximum.

I will post the code here.

#include <queue> #include <iostream> using namespace std; struct comparator { bool operator()(int i, int j) { return i > j; } }; int main(int argc, char const *argv[]) { priority_queue<int, std::vector<int>, comparator> minHeap; minHeap.push(10); minHeap.push(5); minHeap.push(12); minHeap.push(3); minHeap.push(3); minHeap.push(4); while (!minHeap.empty()) { cout << minHeap.top() << " "; minHeap.pop(); } return 0; } 

Could someone please trace the program's execution? I have been trying to understand how this program works for the past couple of hours with no luck.

I got the code from here

I am fairly new to C++ and I was trying to implement a Min Heap using Priority Queue STL in C++. I looked around the web and chanced upon a code, the link to which I have added at the bottom.

I understand how vectors work and I also understand what a functor is but I can't understand how 'return i>j' plays a part in arranging the elements from minimum to maximum.

I will post the code here.

#include <queue> #include <iostream> using namespace std; struct comparator { bool operator()(int i, int j) { return i > j; } }; int main(int argc, char const *argv[]) { priority_queue<int, std::vector<int>, comparator> minHeap; minHeap.push(10); minHeap.push(5); minHeap.push(12); minHeap.push(3); minHeap.push(3); minHeap.push(4); while (!minHeap.empty()) { cout << minHeap.top() << " "; minHeap.pop(); } return 0; } 

Could someone please trace the program's execution? I have been trying to understand how this program works for the past couple of hours with no luck.

I got the code from here

Source Link

Min Heap using STL Priority Queue explanation

I am fairly new to C++ and I was trying to implement a Min Heap using Priority Queue STL in C++. I looked around the web and chanced upon a code, the link to which I have added at the bottom. I understand what a functor is but I can't understand how 'return i>j' plays a part in arranging the elements from minimum to maximum.

I will post the code here.

#include <queue> #include <iostream> using namespace std; struct comparator { bool operator()(int i, int j) { return i > j; } }; int main(int argc, char const *argv[]) { priority_queue<int, std::vector<int>, comparator> minHeap; minHeap.push(10); minHeap.push(5); minHeap.push(12); minHeap.push(3); minHeap.push(3); minHeap.push(4); while (!minHeap.empty()) { cout << minHeap.top() << " "; minHeap.pop(); } return 0; } 

Could someone please trace the program's execution? I have been trying to understand how this program works for the past couple of hours with no luck.

I got the code from here