Home > Net >  How to use comparer function inside class with algorithm header?
How to use comparer function inside class with algorithm header?

Time:11-10

This is what my compare function looks like:

bool smallest_weight(const size_t& i, const size_t& j) {
    return this->abs_weight[i] < this->abs_weight[j];
}

I use this function inside the constructor of the class to initialize some other arrays. This is the code that uses it:

size_t best_node = *min_element(
    this->pointer_list[i   1].begin(),
    this->pointer_list[i   1].end(),
    smallest_weight
);

When I try to compile, I get the following error:

error: invalid use of non-static member function ‘bool TimeCalculator::smallest_weight(const size_t&, const size_t&)’

I can't make the function static since it won't be able to access the data inside the class, I'd also like to avoid making the arrays global if possible.

How can I achieve that?

CodePudding user response:

Try this:

size_t best_node = *min_element(
    this->pointer_list[i   1].begin(),
    this->pointer_list[i   1].end(),
    [&](const auto& i, const auto& j) noexcept { return this->smallest_weight(i, j); }
);
  • Related