C++ 使std::compare函数一元化的简单方法
我需要使用不同的比较函数作为一元函数,其中一个值嵌入到比较器中。为此,我创建了一个适配器类,类似于:C++ 使std::compare函数一元化的简单方法,c++,c++11,C++,C++11,我需要使用不同的比较函数作为一元函数,其中一个值嵌入到比较器中。为此,我创建了一个适配器类,类似于: template<typename T, typename Compare> class CompareAdaptor : private Compare { public: CompareAdaptor(T value) : mValue(value) { } bool operato
template<typename T, typename Compare>
class CompareAdaptor : private Compare
{
public:
CompareAdaptor(T value)
: mValue(value)
{
}
bool operator()(T v) const
{
return Compare::operator()(v, mValue);
}
private:
T mValue;
};
模板
类CompareAptor:私有比较
{
公众:
比较捕捉器(T值)
:mValue(值)
{
}
布尔运算符()(tV)常量
{
返回Compare::operator()(v,mValue);
}
私人:
T值;
};
现在我可以定义一个新的一元比较器,如:
template<typename T>
using EqualTo = CompareAdaptor<T, std::equal_to<T>>;
template<typename T>
using LessEqual = CompareAdaptor<T, std::less_equal<T>>;
模板
使用EqualTo=比较捕捉器;
模板
使用LessEqual=比较捕捉器;
我的问题是:有没有更简单的方法(不使用适配器类)来定义这些一元比较器?我认为这是一个非常常见的问题,您可能有更好的解决方案。在C++11中,这是最好的解决方案。但我更希望谓词直接在调用站点构造:
std::find_if(begin(v), end(v), [limit](int i) { return i < limit; });
std::find_if(begin(v),end(v),[limit](inti){returni
在C++14中,可以使用返回类型推断来生成工厂函数:
template <class Comp>
auto predicate(typename Comp::second_argument_type rhs) {
return [c = Comp{}, rhs](typename Comp::first_argument_type lhs) {
return c(lhs, rhs);
};
}
模板
自动谓词(类型名Comp::第二个参数类型rhs){
返回[c=Comp{},rhs](typename Comp::first_argument_type lhs){
返回c(左、右);
};
}
示例调用:谓词(4)
返回函数对象
在C++11中,这是最好的。但我更希望谓词直接在调用站点构造:
std::find_if(begin(v), end(v), [limit](int i) { return i < limit; });
std::find_if(begin(v),end(v),[limit](inti){returni
在C++14中,可以使用返回类型推断来生成工厂函数:
template <class Comp>
auto predicate(typename Comp::second_argument_type rhs) {
return [c = Comp{}, rhs](typename Comp::first_argument_type lhs) {
return c(lhs, rhs);
};
}
模板
自动谓词(类型名Comp::第二个参数类型rhs){
返回[c=Comp{},rhs](typename Comp::first_argument_type lhs){
返回c(左、右);
};
}
示例调用:谓词(4)
返回函数对象
在C++11中,这是最好的。但我更希望谓词直接在调用站点构造:
std::find_if(begin(v), end(v), [limit](int i) { return i < limit; });
std::find_if(begin(v),end(v),[limit](inti){returni
在C++14中,可以使用返回类型推断来生成工厂函数:
template <class Comp>
auto predicate(typename Comp::second_argument_type rhs) {
return [c = Comp{}, rhs](typename Comp::first_argument_type lhs) {
return c(lhs, rhs);
};
}
模板
自动谓词(类型名Comp::第二个参数类型rhs){
返回[c=Comp{},rhs](typename Comp::first_argument_type lhs){
返回c(左、右);
};
}
示例调用:谓词(4)
返回函数对象
在C++11中,这是最好的。但我更希望谓词直接在调用站点构造:
std::find_if(begin(v), end(v), [limit](int i) { return i < limit; });
std::find_if(begin(v),end(v),[limit](inti){returni
在C++14中,可以使用返回类型推断来生成工厂函数:
template <class Comp>
auto predicate(typename Comp::second_argument_type rhs) {
return [c = Comp{}, rhs](typename Comp::first_argument_type lhs) {
return c(lhs, rhs);
};
}
模板
自动谓词(类型名Comp::第二个参数类型rhs){
返回[c=Comp{},rhs](typename Comp::first_argument_type lhs){
返回c(左、右);
};
}
示例调用:谓词(4)
返回函数对象
@Quentins-answer也可以在C++11中编译,使用
std::function
作为lambda可转换为的返回类型:
template <class Comp>
std::function<typename Comp::result_type (typename Comp::first_argument_type)> predicate(typename Comp::second_argument_type rhs)
{
return [rhs](typename Comp::first_argument_type lhs){
Comp c{};
return c(lhs, rhs);
};
}
模板
函数谓词(类型名Comp::第二个参数类型rhs)
{
返回[rhs](类型名Comp::第一个参数类型lhs){
Comp c{};
返回c(左、右);
};
}
@Quentins-answer也可以在C++11中编译,使用
std::function
作为lambda可转换为的返回类型:
template <class Comp>
std::function<typename Comp::result_type (typename Comp::first_argument_type)> predicate(typename Comp::second_argument_type rhs)
{
return [rhs](typename Comp::first_argument_type lhs){
Comp c{};
return c(lhs, rhs);
};
}
模板
函数谓词(类型名Comp::第二个参数类型rhs)
{
返回[rhs](类型名Comp::第一个参数类型lhs){
Comp c{};
返回c(左、右);
};
}
@Quentins-answer也可以在C++11中编译,使用
std::function
作为lambda可转换为的返回类型:
template <class Comp>
std::function<typename Comp::result_type (typename Comp::first_argument_type)> predicate(typename Comp::second_argument_type rhs)
{
return [rhs](typename Comp::first_argument_type lhs){
Comp c{};
return c(lhs, rhs);
};
}
模板
函数谓词(类型名Comp::第二个参数类型rhs)
{
返回[rhs](类型名Comp::第一个参数类型lhs){
Comp c{};
返回c(左、右);
};
}
@Quentins-answer也可以在C++11中编译,使用
std::function
作为lambda可转换为的返回类型:
template <class Comp>
std::function<typename Comp::result_type (typename Comp::first_argument_type)> predicate(typename Comp::second_argument_type rhs)
{
return [rhs](typename Comp::first_argument_type lhs){
Comp c{};
return c(lhs, rhs);
};
}
模板
函数谓词(类型名Comp::第二个参数类型rhs)
{
返回[rhs](类型名Comp::第一个参数类型lhs){
Comp c{};
返回c(左、右);
};
}
std::bind1st
/std::bind2nd
?std::bind是一个函数调用。它不会创造一种新的类型,一种非常容易理解的类型use@Quentin我在标记列表中添加了C++11:)您能展示一下如何在代码中使用EqualTo
?为什么需要新的比较器成为一个类型?@m.s.我使用的是这样的functor:timeappeased(小于(1.f))或MovesPerformed(等于(5)),其中timeappeased或MovesPerformed接受std::function作为参数std::bind1st
/std::bind2nd
?std::bind是一个函数调用。它不会创造一种新的类型,一种非常容易理解的类型use@Quentin我在标记列表中添加了C++11:)您能展示一下如何在代码中使用EqualTo
?为什么需要新的比较器成为一个类型?@m.s.我使用的是这样的functor:timeappeased(小于(1.f))或MovesPerformed(等于(5)),其中timeappeased或MovesPerformed接受std::function作为参数std::bind1st
/std::bind2nd
?std::bind是一个函数调用。它不会创造一种新的类型,一种非常容易理解的类型use@Quentin我在标记列表中添加了C++11:)您能展示一下如何在代码中使用EqualTo
?为什么需要新的比较器成为一个类型?@m.s.我正在使用这样的函数:timeappead(小于(1.f))或MovesPerformed(等于(5)),其中timeappead或MovesPerformed接受std::function作为参数std::bind1st
/std::bind2nd
?std::