PentiumPro200
PentiumPro200

Reputation: 631

C++ vector iterator nth_element compile error

The code below will not compile. There is an error on the 2nd to the last line (nth_element...). It seems to be related to the comparator. Compiler claims "term does not evaluate to a function taking 2 arguments". How do I fix the compile error?

    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;

Upvotes: 0

Views: 73

Answers (1)

Jarod42
Jarod42

Reputation: 218005

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

should be

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

Upvotes: 3

Related Questions