模板类中的 min_element()
我正在尝试使用模板类构建一个自制的最小堆,以便我可以在 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
I'm trying to build a home-made min-heap with template class so I can work on Dijkstra or Prim. However the find_min() function does not work with std::min_element(). Any clue will be greatly appreciated. Thanks!
the error message from VC2010express, in short, says
error C2780: '_FwdIt std::min_element(_FwdIt,_FwdIt)' : expects 2 arguments - 3 provided
and the code below:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::min_element
的第三个参数是函子对象,或者是指向比较器的函数指针。比较器函数必须是自由函数;您试图为其提供一个非静态
成员函数。由于
compare_node
没有理由成为成员函数,因此您也可以将其设为自由函数。话虽如此,您的
compare_node
相当于std::min_element
的默认比较器的实现,因此您最好根本不使用它。std::min_element
's 3rd argument is either a functor object, or a function pointer to a comparator. The comparator function must be a free function; you're trying to give it a non-static
member function.As there is no reason for
compare_node
to be a member function, you may as well make it a free function instead.Having said that, your
compare_node
is equivalent to the implementation of the default comparator forstd::min_element
, so you may as well not use it at all.正如已经提到的,您可以省略比较器参数,只调用 std::min_element(c.begin(), c.end());。
另一种选择是使用 std::set 而不是向量。集合保持其元素有序,因此您可以通过调用 *theSet.begin(); 来获取最小元素。
As mentioned already you can leave out the comparator argument and just call
std::min_element(c.begin(), c.end());
.Another option would be to use a
std::set
instead of a vector. A set keeps its elements ordered, so you can get the smallest element by calling*theSet.begin();
.