模板类中的Min_element()
min_element() within template class
我试图用模板类构建一个自制的最小堆,这样我就可以在Dijkstra或Prim上工作。然而,find_min()函数不能与std::min_element()一起工作。任何线索将不胜感激。谢谢!
来自VC2010express的错误消息,简而言之,说
错误C2780: '_FwdIt std::min_element(_FwdIt,_FwdIt)':期望2个参数-提供3个
和下面的代码:
#ifndef MIN_HEAP_H
#define MIN_HEAP_H//use (unsorted) vector and min() algorithm
#include <vector>
#include <algorithm>
#include <functional>
template <typename T>
class MinHeap{
std::vector<T> c;//container
typedef typename std::vector<T>::iterator iterator;
bool compare_node(const T& lhs,const T& rhs) const {return lhs<rhs;}//define compare function for nodes
public:
MinHeap():c(){}//default constructor
inline void insert(T node){c.push_back(node);}
iterator find_min(){
iterator min_node=std::min_element(c.begin(),c.end(),compare_node);//doesn't build
return min_node;
}
// deleteMin();
// deleteNode(node);
// decreaseKey(node);
};
#endif
std::min_element
的第三个实参要么是一个函子对象,要么是指向比较器的函数指针。比较器函数必须是自由函数;你试图给它一个非static
成员函数。
由于没有理由将compare_node
作为成员函数,您不妨将其改为自由函数。
话虽如此,您的compare_node
相当于std::min_element
的默认比较器的实现,因此您可能根本不使用它。
如前所述,您可以省略比较器参数,只调用std::min_element(c.begin(), c.end());
。
另一种选择是使用std::set
而不是vector。set保持其元素有序,因此可以通过调用*theSet.begin();
来获取最小的元素。
相关文章:
- 如何使用std::min和std::less返回对象
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 关于 std::min, std::max 中的比较运算符的混淆
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 为什么将 1 添加到 numeric_limits<float>::min() 返回 1?
- 如何将 bsoncxx::d ocument::element 写入控制台
- 在儿童的特定情况下,向下渗透MIN二进制堆失败
- 在f2c.h文件中定义min()max()宏时出错
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- 第 5 行:字符 54:错误:调用"min(int,std::__cxx11::basic_string<char>::size_type)"没有匹配函数
- 如何使用函数std::min()来计算最小值
- 在函数 'int main(int, char**) 中,没有声明 'MIN'
- 将迭代器值与 std::min 一起固定到 end()
- C++是否在 std::min 和 std::max 下标准化 std::optional 的行为?
- [bsoncxx ]如何附加一个bsoncxx::d ocument::element TO bsoncxx::buil
- c++ 中 std::min 的效率
- 当摆脱模偏差时,min = -upper_bound % upper_bound; // 如何工作?
- std :: min-设计考虑
- 如何在 OpenACC 中并行化内部具有"min"功能的循环
- 使用 std::min "no matching function for call to ‘min(<brace-enclosed initializer list>)’"时出错