typedef vector<vector<int> > board;
typedef pair<board,char> vec;
priority_queue<pair<int,vec> > q;
how to arrange in asending order depending on the int value?
CodePudding user response:
You should provide the template argument also for the compare function: in other words, you should state how two pairs of <int, vec> should be compared. Check the documentation here At the moment you are using the std:less of pair, it does what stated here
CodePudding user response:
Nothing in what you currently have should stop a basic priority queue of pair<int,vec>
. However, that queue will order in descending priority (e.g. highest comes first) by using std::less<T>
as the priority determinate.
If you want ascending priority (e.g. lower values are higher-priority), you need to reverse the comparator. I.e. std::greater<T>
. An example appears below:
#include <iostream>
#include <random>
#include <vector>
#include <queue>
typedef std::vector<std::vector<int> > board;
typedef std::pair<board,char> vec;
typedef std::pair<int,vec> elem;
std::priority_queue<elem, std::deque<elem>, std::greater<elem>> q;
int main()
{
std::mt19937 rng{ std::random_device{}() };
std::uniform_int_distribution<> dist(1,100);
// add ten random picks for pair int value
for (int i=1; i<=10; i)
q.push(std::make_pair(dist(rng), vec{}));
while (!q.empty())
{
auto obj = std::move(q.top());
q.pop();
std::cout << obj.first << '\n';
}
}
Output (varies, obviously)
3
4
14
23
40
46
49
53
58
73