Skip to content

Instantly share code, notes, and snippets.

@JeffreyCA
Created August 12, 2017 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyCA/65eed13a197dbe24ad0768be4276508c to your computer and use it in GitHub Desktop.
Save JeffreyCA/65eed13a197dbe24ad0768be4276508c to your computer and use it in GitHub Desktop.
PriorityQueue Wrapper Implementation
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
class PQ {
vector<pair<int, int>> list;
public:
void add(const int value, const int priority) {
pair<int, int> p{value, priority};
vector<pair<int, int>>::iterator i = list.begin();
for (; i != list.end(); ++i) {
if (i->second < priority) {
break;
}
}
list.insert(i, p);
}
class Iterator {
vector<pair<int, int>>::iterator it;
public:
Iterator(vector<pair<int, int>>::iterator it) : it{it} {}
vector<pair<int, int>>::iterator &operator*() {
return it;
}
Iterator &operator++() {
++it;
return *this;
}
bool operator==(const Iterator &other) {
return it == other.it;
}
bool operator!=(const Iterator &other) {
return !(*this == other);
}
};
Iterator begin() {
return Iterator(list.begin());
}
Iterator end() {
return Iterator(list.end());
}
};
int main() {
PQ pq;
pq.add(1, 5);
pq.add(2, 7);
pq.add(24, 2);
pq.add(-12, 10);
for(auto a: pq) {
cout << a->first << ", " << a->second << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment