function template
<functional>

std::bind2nd

template <class Operation, class T>  binder2nd<Operation> bind2nd (const Operation& op, const T& x);
Return function object with second parameter bound
This function constructs an unary function object from the binary function object op by binding its second parameter to the fixed value x.

The function object returned by bind2nd has its operator() defined such that it takes only one argument. This argument is used to call binary function object op with x as the fixed value for the second argument.

It is defined with the same behavior as:

1
2
3
4
5
template <class Operation, class T>
  binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
  return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}

To bind the first parameter to a specific value, see bind1st.

Parameters

op
Binary function object derived from binary_function.
x
Fixed value for the second parameter of op.

Return value

An unary function object equivalent to op but with the second parameter always set to x.
binder2nd is a type derived from unary_function.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// bind2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

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


There are 3 negative elements.


See also