template <class T> inline T absolute(const T &x) { return (x >= 0) ? x : -x; } template <class T> struct absoluteLess : public std::binary_function<T,T,bool> { bool operator()(const T &x,const T &y) const { return absolute(x) < absolute(y); } }; template <class T> struct absoluteGreater : public std::binary_function<T,bool> { bool operator()(T &x,T &y) const { return absolute(x) > absolute(y); } };
编辑:
我以下列方式使用这些功能:
output[j] = *std::max_element(input.begin() + prev,input.begin() + pos,absoluteLess<float>());
输入和输出是一个for循环中的向量
解决方法
如果你想要实际的理由不使用它,请尝试n4190:
unary_function/binary_function were useful helpers when C++98-era
adaptors needed argument_type/etc. typedefs. Such typedefs are
unnecessary given C++11’s perfect forwarding,decltype,and so forth.
(And they’re inapplicable to overloaded/templated function call
operators.) Even if a class wants to provide these typedefs for
backwards compatibility,it can do so directly (at a minor cost in
verbosity) instead of inheriting from unary_function/binary_function,
which is what the Standard itself started doing when these helpers
were deprecated.
现在你根本不需要它,所以你可以从程序中删除它的所有痕迹.
在C14中,增加了透明比较.但它可以在C 11中实现.只是专门为void:
template<> struct absoluteLess<void> { template< class T,class U> constexpr auto operator()( T&& lhs,U&& rhs ) const -> decltype(absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs))) { return absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs)); } } };
现在可以推导出这个类型:
std::max_element(v.begin(),v.end(),absoluteLess<>());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。