I am learning how to use STL in C++. I am trying to implement a heap using std::priority_queue
. Here is my attempt:
#include<vector>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
struct data {
int first;
int second;
};
class compare {
public:
bool operator()(data &a ,data &b)
{
if(a.first < b.first) return true;
else false;
}
};
int main()
{
priority_queue <data , vector<data>, compare> heap;
data temp2[] = { {5,19},{2,7},{90,9},{12,6} };
for(int i = 0; i < 4; ++i)
{
heap.push(temp2[i]);
}
while(heap.empty() == false)
{
cout << heap.top().first << endl;;
heap.pop();
}
}
When I run this code on visual studio 2012. it crashes while pushing the second element. Error is below:
Program: C:\WINDOWS\system32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm
Line: 2463
Expression: invalid operator<
but when I run the same code in ideone, it works but the output is wrong. Ideone link
1.Can someone please point out where I am going wrong?
adding one more question in this same question:
2.How to write the lambda expression of this rather using the compare function?