如果在以下功能发现它们都具有for循环,搜索一个整数位置相同。 Pop()编译,但我得到一个错误的top()必须处理const修饰符。堆类继承自eecs281heap,它存储一个函子Comp Comp,其中Comp是类型名。指导员告诉我们访问这个仿函数的唯一途径就是通过这个 - >(),所以我只是看着一些莅临指导。由于const函数和引用
错误:通过“常量”大“本次”的说法“布尔较大::运算符()(INT,INT)”丢弃预选赛
这发生在运行主INT以下后。通过测试,我已经知道构造函数可以正常工作。
vector <int> data={10,2,13};
poorman_heap<int,larger> y(data.begin(),data.end());
template<typename TYPE, typename COMP>
void poorman_heap<TYPE, COMP>::pop() {
int location=0;
for(int i=1;i<data.size();i++){
if(this->compare(data.at(i),data.at(location))){
location=i;
}
}
data.erase(data.begin()+location);
return;
}
template<typename TYPE, typename COMP>
const TYPE& poorman_heap<TYPE, COMP>::top() const {
int location=0;
for(int i=1;i<data.size();i++){
if(this->compare(data.at(i),data.at(location))){
location=i;
}
}
return data.at(location);
}
P.S.更大的是
struct greater{
bool operator()(int x,int y){
return x>y;
}
}
难道是'比较'不是'const'吗? – juanchopanza
我认为是这样,但我不允许改变任何类的汇编,所以当我通过更大的仿函数进行比较时,我不知道如何访问它与顶部() –
它不会使很有道理。你是说'this-> compare'是一个'更大的'实例吗?如果是这样,你的代码被破坏,因为它假定'TYPE'是'int'。但是您可以即时创建一个本地'更大'的实例,它允许您调用非const方法。 – juanchopanza