求助!关于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'|
#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;//加这两句再看看有什么错,贴出来
#include<iostream>
using std::less;
using std::greater;//加这两句再看看有什么错,贴出来
作者: mingliang1212 发布时间: 2011-11-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28