如何使用比较函数内类与算法头?



我的比较函数是这样的:

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

我在类的构造函数中使用这个函数来初始化其他一些数组。下面是使用它的代码:

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

当我尝试编译时,我得到以下错误:

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

我不能使函数static,因为它将无法访问类内的数据,我也想避免使数组全局如果可能的话。

我怎么才能做到呢?

试试这个:

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); }
);

最新更新