432,175 Members | 1,767 Online
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

# Execute a function in each element of the list conditionally

 P: n/a Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the for_each() algorithm, but it does not let me stop the loop conditionally (in my case, the return value of the functor I pass in). I check transform() also, but that does not work out. Is there any STL algorithm that i can use? If yes, please advice. Thank you. Jan 19 '06 #1
6 Replies

 P: n/a wrote in message news:11********************@o13g2000cwo.googlegrou ps.com... Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the for_each() algorithm, but it does not let me stop the loop conditionally (in my case, the return value of the functor I pass in). I check transform() also, but that does not work out. Is there any STL algorithm that i can use? If yes, please advice. Thank you. You can use 'find' to locate the element which should cause termination of the 'loop'. Save an iterator to that element (let's call it 'found_it') Then use e.g. 'for_each()' with iterator arguments begin() and 'found_it'. This does require two passes through those elements, but it's all I can think of at the moment. (If performance is not impacted, then I'd call it 'good enough'). -Mike Jan 19 '06 #2

 P: n/a Te***********@gmail.com wrote: Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the for_each() algorithm, but it does not let me stop the loop conditionally (in my case, the return value of the functor I pass in). I check transform() also, but that does not work out. Is there any STL algorithm that i can use? If yes, please advice. Thank you. Its not exactly STL, but I would think this could just be solved with a single for() loop; something along the lines of: //assuming vector of ints: for(vector::iterator i = j.begin(); (*i).f1() && i < j.end(); i = i.next()) ; Jan 19 '06 #3

 P: n/a "wdmanegold" wrote in message news:ea*****************@newssvr21.news.prodigy.co m... Te***********@gmail.com wrote: Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the for_each() algorithm, but it does not let me stop the loop conditionally (in my case, the return value of the functor I pass in). I check transform() also, but that does not work out. Is there any STL algorithm that i can use? If yes, please advice. Thank you. Its not exactly STL, but I would think this could just be solved with a single for() loop; something along the lines of: //assuming vector of ints: for(vector::iterator i = j.begin(); (*i).f1() && i < j.end(); i = i.next()) ; A few items of note: 1) an int doesn't have any member functions, so you can't call f1() for it. Since we don't know the class, we can't tell what to put there, but maybe something like vector would illustrate the point better than int. 2) From what I've seen, using "!=" instead of "<" is the usual method for the comparison. Since a vector is a random-access container, I guess that "<" is perfectly legal, but I wonder if it's as efficient, since I would suspect it has to do some kind of "distance" calculation to be sure it's not really ">". 3) What's ".next()"? I don't see that in my book anywhere. Iterators are generally incremented via "++i" (assuming i as the iterator, naturally). So, the example I'd give would be: for (vector::iterator i = j.begin(); (*i).f1() && i != j.end(); ++i); But personally, I'd probably just put a break condition in the body of my for loop, because I find that easier to read than stuffing multiple conditions and other such work into my loop control statement. -Howard Jan 19 '06 #4

 P: n/a * Te***********@gmail.com: Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the for_each() algorithm, but it does not let me stop the loop conditionally (in my case, the return value of the functor I pass in). I check transform() also, but that does not work out. Is there any STL algorithm that i can use? If yes, please advice. 'std::find_if' is probably what you're looking for; just negate your continuation condition. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Jan 19 '06 #5

 P: n/a Howard wrote: "wdmanegold" wrote in message news:ea*****************@newssvr21.news.prodigy.co m...Te***********@gmail.com wrote:Hi,I would like to write a function which does this:for each element in the list call f1() of each element. if f1() return false, break the loop else continueI try to use the for_each() algorithm, but it does not let me stop theloop conditionally (in my case, the return value of the functor I passin).I check transform() also, but that does not work out.Is there any STL algorithm that i can use? If yes, please advice.Thank you.Its not exactly STL, but I would think this could just be solved with asingle for() loop; something along the lines of://assuming vector of ints:for(vector::iterator i = j.begin(); (*i).f1() && i < j.end(); i =i.next()) ; A few items of note: 1) an int doesn't have any member functions, so you can't call f1() for it. Since we don't know the class, we can't tell what to put there, but maybe something like vector would illustrate the point better than int. 2) From what I've seen, using "!=" instead of "<" is the usual method for the comparison. Since a vector is a random-access container, I guess that "<" is perfectly legal, but I wonder if it's as efficient, since I would suspect it has to do some kind of "distance" calculation to be sure it's not really ">". 3) What's ".next()"? I don't see that in my book anywhere. Iterators are generally incremented via "++i" (assuming i as the iterator, naturally). So, the example I'd give would be: for (vector::iterator i = j.begin(); (*i).f1() && i != j.end(); ++i); But personally, I'd probably just put a break condition in the body of my for loop, because I find that easier to read than stuffing multiple conditions and other such work into my loop control statement. -Howard Eep; thanks for catching that. Guess I didn't think it through properly. Jan 19 '06 #6

 P: n/a Howard wrote: 2) From what I've seen, using "!=" instead of "<" is the usual method for the comparison. Since a vector is a random-access container, I guess that "<" is perfectly legal, but I wonder if it's as efficient, since I would suspect it has to do some kind of "distance" calculation to be sure it's not really ">". I don't think there is any performance problem but there can be a maintenence issue. If later you change the type of container to something other than a vector < does not work anymore. The compiler error is this monsterous template malfunction that is very hard to decipher. Once you run into the problem of course you can easily read it, but the first time it happens is a bit of a bummer. Jan 19 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.