fork download
  1. #include <iostream>
  2.  
  3. class Item{
  4. private:
  5. int key;
  6. int data;
  7. public:
  8. Item(): data(0), key(0){}
  9. Item(int data, int key): data(data), key(key){}
  10. void print(){
  11. std::cout << "data: " << data << ", key " << key << std::endl;
  12. }
  13. bool operator <(const Item &item){ return key < item.key; }
  14. bool operator >(const Item &item){ return key > item.key; }
  15. };
  16.  
  17. template<class Item>
  18. class PQ{
  19. private:
  20. Item *pq;
  21. int n;
  22. public:
  23. PQ(int max){
  24. pq = new Item[max];
  25. n = 0;
  26. }
  27. int empty() const{ return n == 0; }
  28. void insert(Item item){
  29. pq[n++] = item;
  30. }
  31. Item getmax(){
  32. int max = 0;
  33.  
  34. for(int i = 1; i < n; i+)
  35. if(pq[max] < pq[i])
  36. max = i;
  37. std::swap(pq[max], pq[n-1]);
  38. return pq[--n];
  39. }
  40. void show(){
  41. for(int i = 0; i < n; i++)
  42. pq[i].print();
  43. }
  44. };
  45.  
  46. int main(){
  47. int p;
  48. Item item;
  49. PQ<Item> pq(50);
  50.  
  51. for(int i = 0; i < 10; i++)
  52. pq.insert(Item(7, i));
  53.  
  54. pq.show();
  55.  
  56. std::cout << "max:\n";
  57. item = pq.getmax();
  58.  
  59. std::cin >> p;
  60. return 0;
  61. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'Item PQ<Item>::getmax()':
prog.cpp:34:27: error: expected primary-expression before ')' token
   for(int i = 1; i < n; i+)
                           ^
stdout
Standard output is empty