sort()的比较函数 [英] Comparison function to sort()
问题描述
我想使用sort()并提供我自己的比较功能,比如
bool lessThan(const S& a,const S& b)
{
返回值(a)<价值(b);
}
然后排序:
sort(a.begin() ,a.end(),lessThan);
所有琐碎的东西,但我有几个不同的指标或价值()
函数,如果你愿意的话。我可以复制和粘贴100次,所以我得到100
的功能如下:
bool lessThan42(const S& a,const S& b)
{
返回值42(a)<价值42(b);
}
但是不是更方便吗?我不能少做一个模板功能
将度量(value()函数)作为模板参数或一些
其他智能的东西将拯救我所有那些复制和粘贴?
/ David
嗨
David Rasmussen写道:
所有琐碎的东西,但我有几个不同的指标或价值()
函数,如果你愿意。我可以复制和粘贴100次,所以我得到100个功能,如:
bool lessThan42(const S& a,const S& b)
{
返回值42 (a)<值42(b);
}
但是,有没有更简单的方法?我不能减少一个模板函数
将度量(value()函数)作为模板参数或其他一些聪明的东西来拯救我复制和粘贴的所有内容吗?
的确,一种可能性是
模板< typename Acc>
struct my_less_cmp
{
Acc acc;
my_less_cmp(Acc acc = Acc())
:acc(acc)
{}
bool operator()(const S& a,const S& b)
{
返回acc(a)< acc(b);
}
};
[...]
std :: sort(a.begin(),a.end(),my_less_cmp< some_accessor>());
Markus
<
Markus Moll写道:
bool operator()(const S& a,const S& b) )
嗯...是的,当然......
bool operator()(const S& a, const S& b)const
Markus
Markus Moll写道:
确实,一种可能性是
模板< typename Acc>
struct my_less_cmp
{
Acc acc;
my_less_cmp(Acc acc = Acc())
:acc(acc)
{}
bool operator()(const S& a,const S& b)
{
返回acc(a) )< acc(b);
}
};
[...]
std :: sort(a.begin(),a。结束(),my_less_cmp< some_accessor>());
好的,但是Acc / some_accessor也必须是一个仿函数,
对吗?我的意思是,我不能只将函数(指针)作为模板参数传递?
/ David
I want to use sort() and supply my own comparison function, like
bool lessThan(const S& a, const S& b)
{
return value(a) < value(b);
}
and then sort by:
sort(a.begin(), a.end(), lessThan);
All trivial stuff, but I have several different metrics or value()
functions if you will. I could copy and paste 100 times so I get 100
functions like:
bool lessThan42(const S& a, const S& b)
{
return value42(a) < value42(b);
}
but isn''t there an easier way? Can''t I make lessThan a template function
that takes the metric (value() function) as a template argument or some
other smart thing that will save me from all that copying and pasting?
/David
Hi
David Rasmussen wrote:
All trivial stuff, but I have several different metrics or value()
functions if you will. I could copy and paste 100 times so I get 100
functions like:
bool lessThan42(const S& a, const S& b)
{
return value42(a) < value42(b);
}
but isn''t there an easier way? Can''t I make lessThan a template function
that takes the metric (value() function) as a template argument or some
other smart thing that will save me from all that copying and pasting?
Indeed, one possibility is
template<typename Acc>
struct my_less_cmp
{
Acc acc;
my_less_cmp(Acc acc = Acc())
: acc(acc)
{}
bool operator()(const S& a, const S& b)
{
return acc(a) < acc(b);
}
};
[...]
std::sort(a.begin(), a.end(), my_less_cmp<some_accessor>());
Markus
Hi again
Markus Moll wrote:
bool operator()(const S& a, const S& b)
Um... yes, of course...
bool operator()(const S& a, const S& b) const
Markus
Markus Moll wrote:
Indeed, one possibility is
template<typename Acc>
struct my_less_cmp
{
Acc acc;
my_less_cmp(Acc acc = Acc())
: acc(acc)
{}
bool operator()(const S& a, const S& b)
{
return acc(a) < acc(b);
}
};
[...]
std::sort(a.begin(), a.end(), my_less_cmp<some_accessor>());
Okay, but then Acc/some_accessor will have to be a functor as well,
right? I mean, I can''t just pass a function (pointer) as template argument?
/David
这篇关于sort()的比较函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!