Created
October 20, 2012 16:14
-
-
Save suzker/3923794 to your computer and use it in GitHub Desktop.
c++ heap/queuee exmaple
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 <vector> | |
#include <algorithm> | |
#include <stdio.h> | |
#include <queue> | |
#include <stdlib.h> | |
using namespace std; | |
class job { | |
public: | |
long cmp; | |
void * stuff; | |
job(long c){ | |
cmp = c; | |
} | |
}; | |
class CompareFoo | |
{ | |
public: | |
// Compare two Foo structs. | |
bool operator()(job& x, job& y) | |
{ | |
return x.cmp > y.cmp; | |
} | |
}; | |
int main(){ | |
// working on heap | |
cout << "working on min heap..." << endl; | |
vector<job> job_v; | |
// insert to heap | |
int i; | |
for (i = 0; i<10; ++i){ | |
job_v.push_back(*(new job(rand()%100+1))); | |
push_heap(job_v.begin(), job_v.end(), CompareFoo()); | |
} | |
// pop from heap | |
for (i = 0; i< 10; ++i){ | |
job front = job_v.front(); | |
printf("job popped cmp = %ld \n", front.cmp); | |
pop_heap(job_v.begin(), job_v.end(), CompareFoo()); | |
job_v.pop_back(); | |
} | |
// queue | |
cout << "working on queue..." << endl; | |
queue<job> job_q; | |
// insert to queue | |
for (i = 0; i<10; ++i){ | |
job_q.push(*(new job(rand()%100+1))); | |
} | |
// pop from queue` | |
for (i=0; i<10; ++i){ | |
job front = job_q.front(); | |
job_q.pop(); | |
cout << "job popped cmp = " << front.cmp << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment