473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

STL bind1st question

I am reading an bind1st example from
http://www.roguewave.com/support/doc...f/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;
}

Jan 13 '06 #1
5 6802
On 13 Jan 2006 14:55:49 -0800 in comp.lang.c++,
si***************@gmail.com wrote,
I am reading an bind1st example from
http://www.roguewave.com/support/doc...f/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));
std::bind1st() already exists.
std::equal_to() already exists.
3 already exists.
Done.
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);


equal_to_3 does not already exist. Somebody has to write it.
Somebody has to test it. Somebody has to maintain it.

Jan 14 '06 #2
In article <11**********************@g14g2000cwa.googlegroups .com>,
si***************@gmail.com wrote:
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?


Yes you can (well, as long as it is a unary predicate :-) ). Be sure to
name it well (as you have in this example). Otherwise the reader has to
stop and look up what your predicate does.

Fwiw, std::tr1::bind (aka boost::bind) is even nicer than std::bind1st
(imho):

using namespace std::tr1::placeholders;

const vector::value_type arr [] = { 1, 2, 3, 4 };

// Set up a vector.
vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);

vector::iterator it1 = std::find_if(v1.begin (),
v1.end (), std::tr1::bind(equal_to(), 3, _1));

I like the flexibility of tr1::bind, and the fact that one facility
(with one name) covers all of the functionality of bind1st, bind2nd, and
much more. So once you learn it, it is easier to remember.

-Howard
Jan 15 '06 #3
Thanks. One more question: what are the advantages of the Boost.bind
library over
the bind functions come with STL?

http://www.boost.org/libs/bind/bind.html

Jan 16 '06 #4

si***************@gmail.com wrote:
Thanks. One more question: what are the advantages of the Boost.bind
library over
the bind functions come with STL?

http://www.boost.org/libs/bind/bind.html


Well for one thing you will commonly want to do something like this.

class Foo
{
std::vector< Bar > myVect;

void fun2( Bar & aBar );

void fun1() // which may be public, it's irrelevant
{
std::for_each( myVect.begin(), myVect.end(), &Foo::fun2 );
}
};

well that's what you'd like to do but that won't compile because fun2
needs a "this" which isn't automatically passed through. I don't think
any of the binders allow you to pass the "this" through. (Do they?)

Now as far as I'm aware with standard binders you can't resolve the
problem (The Bar class doesn't know anything about Foo and mem_fun etc
is for members of Bar, not Foo).
Of course you can always write a functor to solve it, but why should
you have to?

With boost::bind you can (although unfortunately I find this particular
simple case badly documented on boost too).

Jan 16 '06 #5
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Earl Purple" <ea********@gmail.com> wrote:
si***************@gmail.com wrote:
Thanks. One more question: what are the advantages of the Boost.bind
library over
the bind functions come with STL?

http://www.boost.org/libs/bind/bind.html
Well for one thing you will commonly want to do something like this.

class Foo
{
std::vector< Bar > myVect;

void fun2( Bar & aBar );

void fun1() // which may be public, it's irrelevant
{
std::for_each( myVect.begin(), myVect.end(), &Foo::fun2 );
}
};

well that's what you'd like to do but that won't compile because fun2
needs a "this" which isn't automatically passed through. I don't think
any of the binders allow you to pass the "this" through. (Do they?)

Now as far as I'm aware with standard binders you can't resolve the
problem (The Bar class doesn't know anything about Foo and mem_fun etc
is for members of Bar, not Foo).
Of course you can always write a functor to solve it, but why should
you have to?

With boost::bind you can (although unfortunately I find this particular
simple case badly documented on boost too).


Additionally you can do more things with bind such as:

void foo(int x, int y, int z);

for_each(begin(), end(), bind(foo, foo(_1, 2, 3));

or:

bool foo(int x, int y);

unique(begin(), end(), bind(_2, _1));

I.e. you can adapt N-parameter functors to M-parameter functors where N= M, switching the order of parameters as you please. This is far more

powerful than bind1st, bind2nd.

-Howard
Jan 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wenmang | last post by:
Hi, all: I am reading Herb Sutter's article: http://www.cuj.com/documents/s=8840/cujexp0309sutter/ I have trouble to understand for following lines: class Subject { // ... public: virtual...
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
2
by: Fred Ma | last post by:
Hello, I have a random generator that takes a scaling factor as an argument. Since it takes one argument, it is not a generator in the sense defined by SGI's online STL documentation. I'd...
6
by: Greg Lilley | last post by:
I have an application where I want to remove all of the items that are in one vector from a second vector. In the following short program, my objective is to remove all of ourCards from...
6
by: Marc | last post by:
T x; T foo(T, T); bind1st(ptr_fun(foo), x) creates a function object that takes an argument of type T const&. This does not work if T is already a reference type like int const&. So my first...
3
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
2
by: Alberto | last post by:
Hello, while writing a program I ran across the problem of using for_each. Although I can traverse lists with a for loop, I'd prefer to use STL's for_each. Here's my faulty code: #include...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
1
by: pillbug | last post by:
I'm having trouble using bind1st with mem_fun1. I can't compile the line marked below with a @@. If I use the join2 method everything is fine, but I wind up passing string vectors by value. I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.