Next : , Previous : Negators, Top : Table of  Contents


Binders

binder1st Unary Function Object
binder2nd Unary Function Object

binder1st<Operation> bind1st (const Operation& op, const T& x) Function
binder2nd<Operation> bind2nd (const Operation& op, const T& x) Function

Binders bind1st and bind2nd take a function object f of two arguments and a value x and return a function object of one argument constructed out of f with the first or second argument correspondingly bound to x.

template 
class binder1st
: public unary_function {
protected:
    Operation op;
    Operation::first_argument_type value;

public:
    binder1st(const Operation& x, const Operation::first_argument_type& y)
    : op(x), value(y) {}
    result_type operator()(const argument_type& x) const {
        return op(value, x);
    }
};

template 
binder1st bind1st(const Operation& op, const T& x) {
    return binder1st(op, Operation::first_argument_type(x));
}

template 
class binder2nd
: public unary_function {
protected:
    Operation op;
    Operation::second_argument_type value;

public:
    binder2nd(const Operation& x, const Operation::second_argument_type& y)
    : op(x), value(y) {}
    result_type operator()(const argument_type& x) const {
        return op(x, value);
    }
};

template 
binder2nd bind2nd(const Operation& op, const T& x) {
    return binder2nd(op, Operation::second_argument_type(x));
}
For example, find_if(v.begin(), v.end(), bind2nd(greater<int>(), 5)) finds the first integer in vector v greater than 5; find_if(v.begin(), v.end(), bind1st(greater<int>(), 5)) finds the first integer in v less than 5.

 

Top