函数适配器:bind2nd

来源:互联网 发布:华为网络面试 知乎 编辑:程序博客网 时间:2024/06/11 03:48

1.bind2nd使用


2.bind2nd源码

template<class Operation, class T>

inline binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
    typedef typename Operation::second_argument_type arg2_type;
    return binder2nd<Operation>(op, arg2_type(x));
}

template<class Operation>
class binder2nd
            : public unary_function<typename Operation::first_argument_type,
                                    typename Operation::result_type> {
protected:
    Operation op;
    typename Operation::second_argument_type value;
public:
    binder2nd(const Operation& x,
              const typename Operation::second_argument_type& y)
        :op(x), value(y) {
        }
    typename Operation::result_type
    operator(const typename Operation::first_argument_type& x) const {
        return op(x,value);
    }
};


template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
    count_if(InputIterator first, InputIteratorlast,
             Predicate pred) {
    typename
        iterator_traits<InputIterator>::difference_type n = 0;
    for(;first != last; ++ first) {
        if(pred(*first))
            ++n;
    return n;
    }                 
}
原创粉丝点击