S
silverburgh.meryl
I am reading an bind1st example from
http://www.roguewave.com/support/docs/sourcepro/stdlibref/bind1st.html
Can someone please tell me why
// Even better, construct the new predicate on the fly.
vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
is better than
// Now use this new predicate in a call to find_if.
vector::iterator it1 = std::find_if (v1.begin (),
v1.end (),
equal_to_3);
And why I need to use 'std::binder1st' to begin with. Can't I just
create a binary predicate?
Thanks for any help.
Complete code:
int main ()
{
typedef std::vector<int, std::allocator<int> > vector;
typedef std::equal_to<vector::value_type> equal_to;
const vector::value_type arr [] = { 1, 2, 3, 4 };
// Set up a vector.
vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);
// Create an 'equal to 3' unary predicate by binding 3 to
// the equal_to binary predicate.
std::binder1st<equal_to> equal_to_3 =
bind1st (equal_to (), 3);
// Now use this new predicate in a call to find_if.
vector::iterator it1 = std::find_if (v1.begin (),
v1.end (),
equal_to_3);
// Even better, construct the new predicate on the fly.
vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
// And now the same thing using bind2nd.
// Same result since equal_to is commutative.
vector::iterator it3 =
std::find_if (v1.begin (), v1.end (),
std::bind2nd (equal_to (), 3));
// Output results.
std::cout << *it1 << " " << *it2 << " "
<< *it3 << std::endl;
return 0;
}
http://www.roguewave.com/support/docs/sourcepro/stdlibref/bind1st.html
Can someone please tell me why
// Even better, construct the new predicate on the fly.
vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
is better than
// Now use this new predicate in a call to find_if.
vector::iterator it1 = std::find_if (v1.begin (),
v1.end (),
equal_to_3);
And why I need to use 'std::binder1st' to begin with. Can't I just
create a binary predicate?
Thanks for any help.
Complete code:
int main ()
{
typedef std::vector<int, std::allocator<int> > vector;
typedef std::equal_to<vector::value_type> equal_to;
const vector::value_type arr [] = { 1, 2, 3, 4 };
// Set up a vector.
vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);
// Create an 'equal to 3' unary predicate by binding 3 to
// the equal_to binary predicate.
std::binder1st<equal_to> equal_to_3 =
bind1st (equal_to (), 3);
// Now use this new predicate in a call to find_if.
vector::iterator it1 = std::find_if (v1.begin (),
v1.end (),
equal_to_3);
// Even better, construct the new predicate on the fly.
vector::iterator it2 =
std::find_if (v1.begin (), v1.end (),
std::bind1st (equal_to (), 3));
// And now the same thing using bind2nd.
// Same result since equal_to is commutative.
vector::iterator it3 =
std::find_if (v1.begin (), v1.end (),
std::bind2nd (equal_to (), 3));
// Output results.
std::cout << *it1 << " " << *it2 << " "
<< *it3 << std::endl;
return 0;
}