【问题标题】:C++ vector iterator nth_element compile errorC++向量迭代器nth_element编译错误
【发布时间】:2017-07-10 21:28:47
【问题描述】:

下面的代码将无法编译。第 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;

【问题讨论】:

    标签: c++ vector iterator comparator


    【解决方案1】:
    bool cmp(const Result &a, const Result &b)  const;
    

    应该是

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

    【讨论】:

    • 添加解释可能会有所帮助。
    • 感谢 Jarod42,修复了它。
    猜你喜欢
    • 2019-02-14
    • 2015-11-19
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多