|
-
February 15th, 2003, 02:11 AM
#1
Question about bind2nd.
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
-
February 15th, 2003, 04:59 AM
#2
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
-
February 15th, 2003, 05:36 AM
#3
Hi, Paul buddie!
What means "predicate" in your reply?
Thanks in advance,
Geroge
-
February 16th, 2003, 03:24 AM
#4
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
Code:
bool less_than_10(int no)
{
return no < 10;
}
or
Code:
class less_than_10
{
public:
bool operator ()(int no)
{
return no < 10;
}
};
Note: Usually these are made template.
Hope it helps.
Last edited by Zeeshan; February 16th, 2003 at 03:26 AM.
-
February 17th, 2003, 06:01 AM
#5
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
Code:
bool less_than_10(int no)
{
return no < 10;
}
or
Code:
class less_than_10
{
public:
bool operator ()(int no)
{
return no < 10;
}
};
Note: Usually these are made template.
Hope it helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|