自己输入一遍代码,总是胜过仅仅阅读。
template <typename InputIterator, typename Predicate>
InputIterator Find_if(InputIterator& first, InputIterator& end, Predicate pred)
{
while( first != end && !pred (*first))
++ first;
return first;
}
template <typename Arg, typename Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
template <typename Adapertable>
class negate_unary
{
public:
typedef typename Adapertable::argument_type Arg;
typedef typename Adapertable::result_type Result;
negate_unary(const Adapertable& ad):pred(ad){}
bool operator()(const Arg& ag) const{
return !pred(ag);
}
private:
Adapertable pred;
};
template <typename InputIterator, typename Predicate>
InputIterator Find_if(InputIterator& first, InputIterator& end, Predicate pred)
{
while( first != end && !pred (*first))
++ first;
return first;
}
template <typename Arg, typename Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
template <typename Adapertable>
class negate_unary
{
public:
typedef typename Adapertable::argument_type Arg;
typedef typename Adapertable::result_type Result;
negate_unary(const Adapertable& ad):pred(ad){}
bool operator()(const Arg& ag) const{
return !pred(ag);
}
private:
Adapertable pred;
};
本文介绍了一个通用的查找算法实现,该算法使用模板定义了如何在序列中寻找满足特定条件的元素。此外,还展示了一个用于反转谓词适配器的通用模板类,这有助于在不改变原有逻辑的情况下反转条件判断。
447

被折叠的 条评论
为什么被折叠?



