Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Created May 28, 2020 18:07
Show Gist options
  • Save Se7soz/21a4511e2504e2e73617bada2a434c58 to your computer and use it in GitHub Desktop.
Save Se7soz/21a4511e2504e2e73617bada2a434c58 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<functional>
using namespace std;
class Comparator {
public:
bool operator () (const pair<int, function<void()>> p1, const pair<int, function<void()>> p2) {
return p1.first < p2.first;
}
};
struct PriorityQueueWrapper {
priority_queue<pair<int, function<void()>>, std::vector<pair<int, function<void()>>>, Comparator> pq;
void insert(function<void()> f, int score) {
pq.push(make_pair(score, f));
if (pq.size() == 3) {
while (!pq.empty()) {
pq.top().second();
pq.pop();
}
}
}
};
class Foo {
public:
void first(function<void()> printFirst) {
pq.insert(printFirst, 1);
}
void second(function<void()> printSecond) {
pq.insert(printSecond, 2);
}
void third(function<void()> printThird) {
pq.insert(printThird, 3);
}
private:
PriorityQueueWrapper pq;
};
void print1() {
cout << "First";
}
void print2() {
cout << "Second";
}
void print3() {
cout << "Third";
}
main() {
Foo foo;
foo.third(print1);
foo.second(print2);
foo.first(print3);
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment