I am trying to find top n items of a list that gets populated dynamically. I thought priority-queue would be a good way to go about this. Since I need only top n, there is no point in storing anything more than the 'n' items required for my calculation. I am unable to find anything in the boost library that seems to be able to set the limit for the priority queue
I tried using the reserve(element_count) function. The doc said that the function is used to "reserve space for the element_count elements". But that did not work the way I thought it would
This is some sample code that I am writing. This is NOT THE USE CASE though.
int main()
{
int maxSize = 2; // Priority Queue (pq) is expected to hold a maximum of 2 elements at any time
boost::heap::priority_queue<int> pq; // Declaration
pq.reserve(maxSize); // I assumed this would reserve space only for 2 elements and anything more would over write the existing ones based on comparison
pq.push(3);
pq.push(2);
pq.push(1); // Push should fail
cout << "Size = " <<pq.size() << " Max Size = " << (int)pq.max_size();
for (int i=0; i<maxSize; i++)
{
int a = pq.top();
pq.pop();
cout << a <<"\n";
}
return 0;
}
I expected the result to be:
Size = 2 Max Size = 2
3
2
But what I get is:
Size = 3 Max Size = -1
3
2
What am I missing?
Aucun commentaire:
Enregistrer un commentaire