Click to See Complete Forum and Search --> : Question about bind2nd.


George2
February 15th, 2003, 01:11 AM
Hi, everyone!

Here is an example about the usage of
bind2nd. But I do not understand what the
function is doing. I have used MSDN to find
help, but still puzzled because there is only
a little of material on the topic.


Who can tell me what is the function doing in
the following example?


--------
vector<int> v;
// fill v with 4 6 10 3 13 2
int bound = 5;

replace_if (v.begin(), v.end(), bind2nd (less<int>(), bound), bound);

// v: 5 6 10 5 13 5

--------


Thanks in advance,
George

Paul McKenzie
February 15th, 2003, 03:59 AM
The function replaces all values less than 5 with 5. The bind2nd calls less<> with the value of 5, which serves as a predicate to replace_if().

Check your private messages.

Regards,

Paul McKenzie

George2
February 15th, 2003, 04:36 AM
Hi, Paul buddie!

What means "predicate" in your reply?


Thanks in advance,
Geroge

Zeeshan
February 16th, 2003, 02:24 AM
Predicate means a function or function object which return true or false. If it takes one parameter then it is called unary predicate and if it takes two parameter then it is called binary predicate. For example


bool less_than_10(int no)
{
return no < 10;
}


or


class less_than_10
{
public:
bool operator ()(int no)
{
return no < 10;
}
};


Note: Usually these are made template.

Hope it helps.

George2
February 17th, 2003, 05:01 AM
Thanks, Zeeshan buddie!

George

Originally posted by Zeeshan
Predicate means a function or function object which return true or false. If it takes one parameter then it is called unary predicate and if it takes two parameter then it is called binary predicate. For example


bool less_than_10(int no)
{
return no < 10;
}


or


class less_than_10
{
public:
bool operator ()(int no)
{
return no < 10;
}
};


Note: Usually these are made template.

Hope it helps.