Hi, Can you please tell me if there is an easy way to replace the following for loop using boost:lambda? I have a vector of A, i want to call 'aMethod of each A, and stop it when one of them return true. for (vector::const_iterator iter = aList.begin() ; iter != aList.end(); ++iter) { A* anA = (*iter); // break out the for loop if aMethod return true; if (anA->aMethod()){ break; }
Meryl Silverburgh wrote:
Hi,
Can you please tell me if there is an easy way to replace the following for loop using boost:lambda?
I have a vector of A, i want to call 'aMethod of each A, and stop it when one of them return true.
for (vector::const_iterator iter = aList.begin() ; iter != aList.end(); ++iter) {
A* anA = (*iter);
// break out the for loop if aMethod return true; if (anA->aMethod()){ break; }
How about trying
#include
At 12:00 AM -0500 7/27/08, Meryl Silverburgh wrote:
Hi,
Can you please tell me if there is an easy way to replace the following for loop using boost:lambda?
I have a vector of A, i want to call 'aMethod of each A, and stop it when one of them return true.
for (vector::const_iterator iter = aList.begin() ; iter != aList.end(); ++iter) {
A* anA = (*iter);
// break out the for loop if aMethod return true; if (anA->aMethod()){ break; }
Isn't this what std::find_if does? std::find_if ( aList.begin (), aList.end (); bind ( A::Method, *_1 )); [ Code not compiled, just composed in email client, but you get the idea ] http://www.boost.org/doc/libs/1_35_0/doc/html/lambda/le_in_details.html#memb... -- -- Marshall Marshall Clow Idio Software mailto:marshall@idio.com It is by caffeine alone I set my mind in motion. It is by the beans of Java that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning. It is by caffeine alone I set my mind in motion.
AMDG Marshall Clow wrote:
At 12:00 AM -0500 7/27/08, Meryl Silverburgh wrote:
for (vector::const_iterator iter = aList.begin() ; iter != aList.end(); ++iter) {
A* anA = (*iter);
// break out the for loop if aMethod return true; if (anA->aMethod()){ break; }
Isn't this what std::find_if does?
std::find_if is not quite equivalent to the for loop, because std::find_if is not allowed to modify the input sequence. In Christ, Steven Watanabe
On Mon, Jul 28, 2008 at 7:01 PM, Steven Watanabe
AMDG
Marshall Clow wrote:
At 12:00 AM -0500 7/27/08, Meryl Silverburgh wrote:
for (vector::const_iterator iter = aList.begin() ; iter != aList.end(); ++iter) {
A* anA = (*iter);
// break out the for loop if aMethod return true; if (anA->aMethod()){ break; }
Isn't this what std::find_if does?
std::find_if is not quite equivalent to the for loop, because std::find_if is not allowed to modify the input sequence.
Ok, here's myquestion: What does exactly mean that std::find_if is not allowed to change the sequence? How does the code. It is not changing the vector in any way (just using a constant iterator) it is not even modifying the contents of the vector. It is just calling a (possibly not constant) method through a (constant) pointer. To me that is not modifying the input sequence. Or is it more a general concern that they are not equivalent even if in this case std::find_if could be used? David
AMDG David Rodríguez Ibeas wrote:
Ok, here's myquestion: What does exactly mean that std::find_if is not allowed to change the sequence? How does the code. It is not changing the vector in any way (just using a constant iterator) it is not even modifying the contents of the vector. It is just calling a (possibly not constant) method through a (constant) pointer. To me that is not modifying the input sequence.
Or is it more a general concern that they are not equivalent even if in this case std::find_if could be used?
To be precise the standard states: "The function object pred shall not apply any non-constant function through the dereferenced iterator." I presume that non-constant means anything that could make pred(x) != pred(x) be true. In Christ, Steven Watanabe
participants (5)
-
David Rodríguez Ibeas
-
Marshall Clow
-
Meryl Silverburgh
-
Niko Vuokko
-
Steven Watanabe