gpt4 book ai didi

C++ vector 迭代器 nth_element 编译错误

转载 作者:行者123 更新时间:2023-11-27 23:52:51 26 4
gpt4 key购买 nike

下面的代码将无法编译。第 2 行到最后一行 (nth_element...) 有错误。这似乎与比较器有关。编译器声称“术语不计算为采用 2 个参数的函数”。如何修复编译错误?

    struct Result {
Result(unsigned int id, double result);
bool cmp(const Result &a, const Result &b) const;

unsigned int id;
double result;
};


Result::Result(unsigned int id, double result) {
this->id = id;
this->result = result;
}

bool Result::cmp(const Result &a, const Result &b) const {
if(a.result < b.result) {
return true;
}
return false;
}

//25th-percentile
int index = (int) ((buffer.size()+1.0)/4.0 - 0.499);
vector<Result>::iterator itrindex = buffer.begin() + index;
nth_element(buffer.begin(), itrindex, buffer.end(), &Result::cmp);
double twentyfifthperc = buffer[index].result;

最佳答案

bool cmp(const Result &a, const Result &b)  const;

应该是

static bool cmp(const Result &a, const Result &b);

关于C++ vector 迭代器 nth_element 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45021900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com