I just started Data Structures and i have stumbled upon Priority queues, I wrote a simple program to print the values that i have pushed in the Queues but it wont print anything.
#include<iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> maxi;
priority_queue<int, vector<int>, greater<int>> mini;
int m = maxi.size();
int n = mini.size();
maxi.push(1);
maxi.push(3);
maxi.push(2);
maxi.push(5);
maxi.push(0);
cout<<"Max size->"<<maxi.size()<<endl;
for(int i=0; i<m; i++){
cout<<maxi.top()<<" ";
maxi.pop();
}cout<<endl;
mini.push(1);
mini.push(3);
mini.push(2);
mini.push(5);
mini.push(0);
cout<<"Mini size->"<<mini.size()<<endl;
for(int j=0; j<n; j++){
cout<<mini.top()<<" ";
mini.pop();
}cout<<endl;
}
I have read so many articles i just dont seem to find any error. Even the Compiler doesnt give any error.
You are getting the size of maxi and mini before adding elements to them. Since you're copying the value of the size (explained here), it wouldn't be updated when adding elements to the containers. Therefore your loop is not executed at all.