根据特定字段对结构向量进行排序 [英] Sort a vector of struct based on a specific field
本文介绍了根据特定字段对结构向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在尝试根据特定字段对结构向量进行排序.我已经为排序功能的使用设置了自定义比较功能.但是,我遇到了一些错误.
Currently I am trying to sort a vector of structs based on a specific field. I have set up a custom comparison function for the use of the sort function. However, i am getting some errors with it.
代码:
struct Play{
int min, down, yard, locat;
string Description, offname, defname;
double relevance;
};
bool customCompare(const Play &x, const Play &y)
{
return (x.relevance < y.relevance);
}
void printResults()
{
sort(vecData.begin(),vecData.end(), customCompare);
}`
错误:
error C3867: 'List::customCompare': function call missing argument list; use '&List::customCompare' to create a pointer to member
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
推荐答案
a)使用具有lambda表示法的排序函数,如下所示(如果您使用的是c ++ 11)
a) Use sort function with lambda notation as below( if you are using c++11)
sort(vecData.begin(),vecData.end(), [](const Play &x, const Play &y){ return (x.relevance < y.relevance);});
工作代码:
b)使比较器具有静态功能
b) Make comparator function as static
这篇关于根据特定字段对结构向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文