【发布时间】:2013-04-10 12:33:54
【问题描述】:
我尝试为类重载< 运算符并调用函数如下:
bool Edge::operator<(Edge const & e) const {
return this->GetCost() < e.GetCost();
}
在main()中
sort(edge_set.begin(),edge_set.end());
此外,我还尝试为对象编写一个简单的比较器函数,在 main.cpp 中定义并尝试调用sort(),但再次失败:
bool edge_comparator(Edge& e1, Edge& e2){
return (e1.GetCost() < e2.GetCost());
}
在main()中
sort(edge_set.begin(),edge_set.end(), edge_comparator);
对于我尝试过的那些,我得到一个编译错误。我在这里做错了什么?如何对对象集进行排序?
【问题讨论】:
-
edge_set是如何定义的? -
edge_set 是如何定义的?如果 edge_set 是
std::set,那么 它已经排序了。