Following code uses same comparator function with vector and priority queue. However order produced by both data structures is different. I would like priority queue to behave in same way as vector.
I have two questions
Here's the output of following code:
//Please ignore extra header files, I know I don't need them.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <iterator>
#include <unordered_map>
#include <functional>
using namespace std;
class Solution {
public:
typedef pair<string, int> PII;
static bool cmp(const PII& a, const PII& b)
{
if (a.second == b.second)
return a.first < b.first;
return a.second > b.second;
}
void func(vector<string>& words)
{
unordered_map<string, int> hMap;
for (const auto& w : words)
hMap[w]++;
std::priority_queue< PII, std::vector<PII>, std::function<bool(PII, PII)> > Q(cmp);
vector<PII> V;
for (const auto& e : hMap)
{
Q.emplace(e);
V.emplace_back(e);
}
std::sort(V.begin(), V.end(), cmp);
//Now why does order of elements is different in vector V and priority_queue Q, despite using same comparator function?
int size = Q.size();
cout << "Order in priority Queue:" << endl;
for (int i = 0; i < size; i++)
{
PII e = Q.top();
cout << e.first << ":" << e.second << endl;
Q.pop();
}
cout << "Order in vector:" << endl;
for (const auto& e : V)
{
cout << e.first << ":" << e.second << endl;
}
}
};
int main()
{
Solution s;
vector<string> words = {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is" , "we" , "we" , "we" };
s.func( words );
return 0;
}
Priority Queues and vectors use comparators differently. To understand the output of priority queue, you must understand its working. Priority Queue is effectively a heap with an element on top depending on the comparison function. To quote from boost Priority Queue
The comparison function used to determine whether one element is smaller than another element. If Compare(x,y) is true, then x is smaller than y. The element returned by Q.top() is the largest element in the priority queue. That is, it has the property that, for every other element x in the priority queue, Compare(Q.top(), x) is false.
In your case changing the comparison function to reverse the order should solve the issue.