【发布时间】:2014-02-01 22:25:21
【问题描述】:
这可能是一个愚蠢的问题,但我写了如下代码。
void someFunction() {
struct sort_pred {
inline bool operator()(const std::pair<int,double> &left, const std::pair<int,double> &right) const {
return left.second < right.second;
}
};
std::sort(regionAreas.begin(), regionAreas.end(), sort_pred());
}
但是,这并不能编译说,
///:1542: error: no matching function for call to 'sort(std::vector<std::pair<int, double> >::iterator, std::vector<std::pair<int, double> >::iterator, someFunction::sort_pred)'
如何在函数中使用结构体作为比较器? 或者,不可能吗?
【问题讨论】:
-
您使用的是什么编译器和编译器版本?我使用 Visual Studio 2005 做了一个简单的控制台应用程序,这个结构在
std::vector排序下编译并运行良好。根据Visual Studio support for new C/C++ standards? 和Which standard does VS2005, VS2008 follow? VS 2005 的目标是C++03。
标签: c++ struct std comparator stl-algorithm