Checking C++ priority_queue API, the user-provided Compare only specified the rules of reordering, but not specified when to reinforce the ordering.
For example, say I am using priority_queue to get the max occurrence of char in a string.
Assume the string is "cab", prioritu_queue has c, 1 now. And now I want to insert a,1, then b,1 into it :
auto cmp = [&](const char& letter1, const char& letter2) {
return v[letter1 - 'a'] < v[letter2 - 'a'];
};
priority_queue<char, vector<char>, decltype(cmp)> q{cmp};
Quoted from official API doc
A Compare type providing a strict weak ordering. Note that the Compare parameter is defined such that it returns true if its first argument comes before its second argument in a weak ordering. However, because the priority queue outputs largest elements first, the elements that "come before" are actually output last. That is, the front of the queue contains the "last" element according to the weak ordering
Does this mean that the above operation will return b to me ( as b,1 comes last, and all three elements in PQ are equally counted such that a weak ordering)?
And if so, the only way to get a ( alphabetic order) will be that I re-write cmp to enforce it consider alphabetic order, in addition to char occurrence count?
PS, I do have tried to experiment myself: https://onlinegdb.com/YDjOcABJh
This is the code I use to test if b comes or c comes first, the experiment result is c, not b!
I am confused on the API wording now, any help is appreciated!
Thanks!