Created
August 12, 2017 21:27
-
-
Save JeffreyCA/65eed13a197dbe24ad0768be4276508c to your computer and use it in GitHub Desktop.
PriorityQueue Wrapper Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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