473,568 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterato r 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::iterato r 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<v ector::value_ty pe> equal_to;

const vector::value_t ype 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::iterato r it1 = std::find_if (v1.begin (),
v1.end (),
equal_to_3);

// Even better, construct the new predicate on the fly.
vector::iterato r 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::iterato r 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 6810
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::iterato r 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::iterato r 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************ **********@g14g 2000cwa.googleg roups.com>,
si************* **@gmail.com wrote:
Can someone please tell me why
// Even better, construct the new predicate on the fly.
vector::iterato r 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::iterato r 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::place holders;

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

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

vector::iterato r 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************ **********@g43g 2000cwa.googleg roups.com>,
"Earl Purple" <ea********@gma il.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
5283
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 void Attach( function<void (Subject*)> o ) { obs_.push_back(o); } --Line1
3
5014
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
2253
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 like to use this as a generator in the STL algorithm "generate". I know I have to change random generator from its current implementation to make it...
6
2671
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 cardsAvailable without writing a loop. I have tried a number of different variations using the for_each algorithm without success, and am currently getting...
6
2204
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 problem is with the &. My second problem is with the const. Why should bind1st change the constness of the second argument of the function ?
3
1767
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
2
2127
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 <iostream> #include <list> #include <algorithm>
3
3071
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 before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
1
5799
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 would like to use the reference passing version, or something similar. The errors I get point the finger at binder1st::operator() being defined as...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.