Monday, May 23, 2016

priority queue implementation in c++

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <queue>
using namespace std;
class Foo
{
  public:
int key;
int value;
};

class Compare
{
public:
    bool operator() (Foo Foo1, Foo Foo2)
    {
        return Foo1.value < Foo2.value;
    }
};

int main()
{
    srand(time(NULL));
    std::priority_queue<Foo, std::vector<Foo>, Compare> pq;
    for (int i=0;i<5;i++){
Foo tmp;
tmp.key = i;
tmp.value = rand()%100;
        printf("key %d = value %d\n",tmp.key, tmp.value);
pq.push(tmp);
    }
    while (!pq.empty()){
      Foo tmp = pq.top();
printf("%d = %d\n",tmp.key,tmp.value);
pq.pop();
}
    return 0;
}

output
=====
Success time: 0 memory: 3456 signal:0
key 0 = value 76
key 1 = value 62
key 2 = value 57
key 3 = value 74
key 4 = value 74
0 = 76
3 = 74
4 = 74
1 = 62
2 = 57

No comments:

Post a Comment