+ -
当前位置:首页 → 问答吧 → 求助!关于less与greater的用法

求助!关于less与greater的用法

时间:2011-11-30

来源:互联网

#include<functional>
#include<iostream>

template<class T,class cmp= less <T> >
class priorityQueue{
  public:
  priorityQueue(int capacity=100){
  array=new T[capacity];
  maxSize=capacity;
  currentSize=0;}

  priorityQueue(const T data[],int size)
  :maxSize(size+10),currentSize(size){
  array=new T[maxSize];
  for(int i=0;i<size;i++)array [i+1]=data[i];
  buildHeap();
  }
  ~priorityQueue(){delete []array;}

  bool isEmpty()const {return currentSize==0;}

  void enQueue(const T&x){
  if(currentSize==maxSize-1)doubleSpace();
  int hole=++currentSize;
  for(;hole>1&&cmp()(x,array[hole/2]);hole/=2)
  array[hole]=array[hole/2];
  array[hole]=x;
  }

  T deQueue(){
  T minItem=array[1];
  array[1]=array[currentSize--];
  percolateDown(1);
  return minItem;
  }

  T getHead()const
  {
  return array[1];
  }
private:
  int currentSize;
  T*array;
  int maxSize;

  void doubleSpace(){
  T *tmp=array;

  maxSize*=2;
  array=new T[maxSize];
  for (int i=0;i<=currentSize;i++)array[i]=tmp[i];
  delete[] tmp;
  }

  void buildHeap()
  {
  for(int i=currentSize/2;i>0;i--)percolateDown(i);
  }

  void percolateDown(int hole){
  int child;
  T tmp=array[hole];

  for(;hole*2<=currentSize;hole=child){
  child=hole*2;
  if (child!=currentSize
  &&cmp()(array[child+1],array[child]))
  child++;
  if(cmp()(array[child],tmp))array[hole]=array[child];
  else break;
  }
  array[hole]=tmp;
  }
};

template<class T>
T GetKth(T a[],int N,int K)
{ priorityQueue<T,greater<T>>hp(a,N);

T ret;

for(int i=0;i<K;i++)ret=hp.deQueue();
return ret;
}

template<class T>
T GetKth2(T a[],int N,int K)
{
  priorityQueue<T> H(a,K);

  for (int i=K;i<N;i++){
  if(H.getHead()<a[i]){
  H.deQueue();
  H.enQueue(a[i]);
  }
  return H.getHead();
}

这是头文件,gcc编译怎么都过不了,问题还蛮多的,但我觉得就类模版的使用出现问题了导致下面都有问题。。求助。。
D:\C++\projects\10_7\priorityQueue.h|6|error: expected type-specifier before 'less'|
D:\C++\projects\10_7\priorityQueue.h|6|error: expected '>' before 'less'|
D:\C++\projects\10_7\priorityQueue.h||In function 'T GetKth(T*, int, int)':|
D:\C++\projects\10_7\priorityQueue.h|80|error: 'greater' was not declared in this scope|
D:\C++\projects\10_7\priorityQueue.h|80|error: 'N' cannot appear in a constant-expression|
D:\C++\projects\10_7\priorityQueue.h|80|error: a function call cannot appear in a constant-expression|
D:\C++\projects\10_7\priorityQueue.h|80|error: template argument 2 is invalid|
D:\C++\projects\10_7\priorityQueue.h|84|error: 'hp' was not declared in this scope|
D:\C++\projects\10_7\priorityQueue.h||In function 'T GetKth2(T*, int, int)':|
D:\C++\projects\10_7\priorityQueue.h|91|error: template argument 2 is invalid|
D:\C++\projects\10_7\priorityQueue.h|91|error: invalid type in declaration before '(' token|
D:\C++\projects\10_7\priorityQueue.h|94|error: request for member 'getHead' in 'H', which is of non-class type 'int'|
D:\C++\projects\10_7\priorityQueue.h|95|error: request for member 'deQueue' in 'H', which is of non-class type 'int'|
D:\C++\projects\10_7\priorityQueue.h|96|error: request for member 'enQueue' in 'H', which is of non-class type 'int'|
D:\C++\projects\10_7\priorityQueue.h|98|error: request for member 'getHead' in 'H', which is of non-class type 'int'|

作者: lzckean   发布时间: 2011-11-30

#include<functional>
#include<iostream>

using std::less;
using std::greater;//加这两句再看看有什么错,贴出来

作者: mingliang1212   发布时间: 2011-11-30