Using boost bind library to loop thru a list

I have a int vector, and I would like to loop thru the vector and call a function which takes an int (in this case, I want to print). void A::aFunction() { vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4); // want to loop thru the list and call A::print(int i)? } void A::print(int i) { } how can I do that using boost bind library?

boost::bind isn't necessary for this.
std::copy(
aVector.begin(),
aVector.end(),
std::ostream_iterator< std::string >(std::cout, "\n"));
If you really want to use boost::bind, it would be something like:
std::for_each(
aVector.begin(),
aVector.end(),
boost::bind(
&A::print,
_1 ) );
although I haven't tried it out myself.
On 2/14/06, Meryl Silverburgh
I have a int vector, and I would like to loop thru the vector and call a function which takes an int (in this case, I want to print).
t void A::aFunction() {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i)? }
void A::print(int i) {
}
how can I do that using boost bind library?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Thanks how can I call a function with 2 more parameters, like this:
How can I pass 'param1', 'param2' to the fuction doStuff?
void A::aFunction( float param1, float param2) {
vector<int> aVector;
aVector.push_back(1);
aVector.push_back(2);
aVector.push_back(3);
aVector.push_back(4);
// want to loop thru the list and call A::print(int i, float param1,
float param2)?
}
void A::doStuff(int i, float param1, float param2) {
}
On 2/14/06, Noel Yap
boost::bind isn't necessary for this.
std::copy( aVector.begin(), aVector.end(), std::ostream_iterator< std::string >(std::cout, "\n"));
If you really want to use boost::bind, it would be something like:
std::for_each( aVector.begin(), aVector.end(), boost::bind( &A::print, _1 ) );
although I haven't tried it out myself.
On 2/14/06, Meryl Silverburgh
wrote: I have a int vector, and I would like to loop thru the vector and call a function which takes an int (in this case, I want to print).
t void A::aFunction() {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i)? }
void A::print(int i) {
}
how can I do that using boost bind library?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

How can I copy elements from one STL container to another STL container if a condition is met and if it find an element fails that condition, it stops the copying? I wonder if Boost has library for that. I don't think the original STL algorithm handle this case. Thank you. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

In Stroustrup's book "The C++ Programming Language -- 3rd edition", 18.6 is "Modifying Sequence Algorithms". 18.6.1 is "Copy". He talks about what is in the standard and then gives an example almost what you are asking for. You could also use the standard Copy (described in 25.2.1 of the standard) and compute the "InputInterator last" using your criterial before doing the copy. Yes, that does make two passes but any test inside the copy loop will cost almost the same. On Feb 15, 2006, at 8:58 PM, yinglcs2@yahoo.com wrote:
How can I copy elements from one STL container to another STL container if a condition is met and if it find an element fails that condition, it stops the copying?
I wonder if Boost has library for that. I don't think the original STL algorithm handle this case.
Thank you.
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Meryl Silverburgh wrote:
Thanks how can I call a function with 2 more parameters, like this: How can I pass 'param1', 'param2' to the fuction doStuff?
void A::aFunction( float param1, float param2) {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i, float param1, float param2)?
for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
}
void A::doStuff(int i, float param1, float param2) {
}

Thanks. One more question, why the function 'doStuff' has to be 'public' otherwise, it won't compile? In my example, aFunction is a member function of 'A', so I can put 'doStuff' as private, right? but that won't compile.
for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
Can someone please tell me why?
On 2/16/06, Peter Dimov
Meryl Silverburgh wrote:
Thanks how can I call a function with 2 more parameters, like this: How can I pass 'param1', 'param2' to the fuction doStuff?
void A::aFunction( float param1, float param2) {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i, float param1, float param2)?
for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
}
void A::doStuff(int i, float param1, float param2) {
}
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Meryl Silverburgh wrote:
Thanks. One more question, why the function 'doStuff' has to be 'public' otherwise, it won't compile? In my example, aFunction is a member function of 'A', so I can put 'doStuff' as private, right? but that won't compile.
for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
Can someone please tell me why?
Your compiler is broken? :-) It works under MSVC 7.1, g++ 3.4.4 and Comeau 4.3.0.1.

Rearranged reply: Meryl Silverburgh wrote:
Meryl Silverburgh wrote:
Thanks how can I call a function with 2 more parameters, like this: How can I pass 'param1', 'param2' to the fuction doStuff?
void A::aFunction( float param1, float param2) {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i, float param1, float param2)?
for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
}
void A::doStuff(int i, float param1, float param2) {
} Thanks. One more question, why the function 'doStuff' has to be 'public' otherwise, it won't compile? In my example, aFunction is a member function of 'A', so I can put 'doStuff' as private, right? but
On 2/16/06, Peter Dimov
wrote: that won't compile. for_each( aVector.begin(), aVector.end(), bind( &A::doStuff, this, _1, param1, param2 ) );
Can someone please tell me why?
Not sure what your exactly asking here. for_each is expecting to call a function f( value_type& ), where in this case value type is an int. for_each call this function f for each derefrenced iterator in the sequence v.begin() to v.end(). The bind call above returns such a function, mapping the int in f, to the placeholder in the call to A::doStuff. The assumption is made that A is a class rather than a name space, since you didn't post a complete example. So that makes A::doStuff a member function. Member functions take a this pointer as their hidden 1st argument. A::aFunction is assumed to be a member function as well so the this pointer is available. bind( &A::doStuff // an A member function , this // this instance of A's this pointer , _1 // the derefenced vector::iterator (an int ) , param1 // the 1st float arg value , param2 // the 2nd float arg value ) Jeff Flinn

On 2/14/06, Meryl Silverburgh
I have a int vector, and I would like to loop thru the vector and call a function which takes an int (in this case, I want to print).
void A::aFunction() {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i)? }
void A::print(int i) {
}
how can I do that using boost bind library?
Easiest way is A anA; std::for_each(aVector.begin(), aVector.end(), boost::bind(&A::print, &anA, _1)); HTH Stuart Dootson

On 2/15/06, Stuart Dootson
On 2/14/06, Meryl Silverburgh
wrote: I have a int vector, and I would like to loop thru the vector and call a function which takes an int (in this case, I want to print).
void A::aFunction() {
vector<int> aVector; aVector.push_back(1); aVector.push_back(2); aVector.push_back(3); aVector.push_back(4);
// want to loop thru the list and call A::print(int i)? }
void A::print(int i) {
}
how can I do that using boost bind library?
Easiest way is
A anA; std::for_each(aVector.begin(), aVector.end(), boost::bind(&A::print, &anA, _1));
HTH Stuart Dootson
Use 'this' if you're doing the loop in A::aFunction... Stuart
participants (7)
-
Jeff Flinn
-
Meryl Silverburgh
-
Noel Yap
-
Perry Smith
-
Peter Dimov
-
Stuart Dootson
-
yinglcs2@yahoo.com