cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : binder2nd
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_fu...
· pointer_to_unary_fun...
· unary_negate

-

binder2nd class template
template <class Operation> class binder2nd;
<functional>

Generate function object class with 2nd parameter binded

Generates an unary function object class from the binary object class Operation by binding its second parameter to a fixed value.

binder2nd is generally used as a type. The function bind2nd (also defined in header <functional>) can be used to directly construct an object of this type.

binder2nd is constructed using a binary function object as argument. A copy of this object is used by its member operator() to generate a result from its parameter and the fixed value set on construction.

This class is derived from unary_function and is typically defined as:

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::first_argument_type& y) : op (x), value(y) {}
  typename Operation::result_type
    operator() (const typename Operation::first_argument_type& x) const
    { return op(x,value); }
};

binder2nd class is specifically designed to bind function objects (operations) derived from binary_function (it requires member first_argument_type and second_argument_type).

Members

constructor
Constructs an unary function object class from a binary function object by binding its second argument to a value.
operator()
Member function taking a single parameter and returning the result of calling the binary function object used at construction with its second argument binded to a specific value.

Example

// binder2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  binder2nd < less<int> > IsNegative (less<int>(),0);
  int numbers[] = {10,-20,-30,40,-50};
  int cx;
  cx = count_if (numbers,numbers+5,IsNegative);
  cout << "There are " << cx << " negative elements.\n";
  return 0;
}

Output:


There are 3 negative elements.

See also

bind2nd Return function object with second parameter binded (function template)
binder1st Generate function object class with 1st parameter binded (class template)
unary_function Unary function object base class (class template)
binary_function Binary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us