Reputation: 19
I want to array "functions" by priority
E.g SetNumber1 is first SetNumber2 is second ReadNumbers is last
////////////////
priority_queue < ??? > Q
Q.push(ReadNumbers());
Q.push(SetNumber2());
Q.push(SetNumber1());
i want to exec in order to SetNumber1() , SetNumber2(), ReadNumbers()
Thanks.
and sorry about my english skils, I'am korean, i'm not good at english
Upvotes: 0
Views: 177
Reputation: 14392
std::priority_queue works with a compare function which can't work with the given functions. You need to define your own priorities as an enum and then you can put the pair of the prio and function in an std::multimap so all functions can be called according to their prio. You can put the pair also in a prio-q but then you still need a compare function that only works on the prio.
e.g.:
#include <iostream>
#include <map>
#include <functional>
enum class prio { HI, MID, LO };
int main()
{
std::multimap<prio, std::function<void()>> prio_mmap;
prio_mmap.insert(std::make_pair(prio::MID, []{ std::cout << "MID1\n"; }));
prio_mmap.insert(std::make_pair(prio::LO, []{ std::cout << "LO\n"; }));
prio_mmap.insert(std::make_pair(prio::HI, []{ std::cout << "HI\n"; }));
prio_mmap.insert(std::make_pair(prio::MID, []{ std::cout << "MID2\n"; }));
for (const auto& p: prio_mmap)
{
p.second();
}
}
Upvotes: 2