這是我的比較函式的樣子:
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,因為它無法訪問類中的資料,我還想盡可能避免將陣列設為全域。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
嘗試這個:
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); }
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532371.html
標籤:C 算法stl
上一篇:合并串列項的優化方案
