将 3 个函子合并为 1 个
我有 3 个函子,想知道是否可以将它们组合成 1 个,也许作为模板。 是否可以?如果是这样,我会怎么做。谢谢!
struct less_than
{
bool operator()(double prev,double curr) const
{
return prev<curr;
}
};
struct great_than
{
bool operator()(double prev,double curr) const
{
return prev>curr;
}
};
struct equal_to
{
bool operator()(double prev, double curr) const
{
return prev==curr;
}
};
I have 3 functors and was wondering if these can be combined into 1, perhaps as a template.
is it possible? if so, how would I do it. thx!
struct less_than
{
bool operator()(double prev,double curr) const
{
return prev<curr;
}
};
struct great_than
{
bool operator()(double prev,double curr) const
{
return prev>curr;
}
};
struct equal_to
{
bool operator()(double prev, double curr) const
{
return prev==curr;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的意思是由运营商专门化,那么答案是,不,不是在语言级别。
幸运的是,STL 已经为此提供了函子(
std::equal_to
等)。您可以直接使用它们,也可以将它们用作您自己的函数类的参数。If you mean, specialized by the operator, then the answer is, no, not at the language level.
Luckily, the STL already provides functors for this (
std::equal_to
, etc.). You can either use these directly, or use them as arguments to your own function classes.由于这些都存在于标准库中,因此您可以执行
并使用例如
compare<; std::less<双>; >
。但这毫无用处,因为您可以直接使用标准库函子。As these are all existent in the standard library, you can just do
And use e.g.
compare< std::less<double> >
. But this would be quite useless, as you can just use the standard library functors directly.你可以做这样的事情:
使用它:
You can do something like this:
use it: