473,809 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Execute a function in each element of the list conditionally

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 1880

<Te***********@ gmail.com> wrote in message
news:11******** ************@o1 3g2000cwo.googl egroups.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
Te***********@g mail.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<int> ::iterator i = j.begin(); (*i).f1() && i < j.end(); i =
i.next()) ;
Jan 19 '06 #3

"wdmanegold " <wd********@gma il.com> wrote in message
news:ea******** *********@newss vr21.news.prodi gy.com...
Te***********@g mail.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<int> ::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<myClass> 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<int>::i terator 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
* Te***********@g mail.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
Howard wrote:
"wdmanegold " <wd********@gma il.com> wrote in message
news:ea******** *********@newss vr21.news.prodi gy.com...
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<in t>::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<myClass> 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<int>::i terator 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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
5686
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. Not being a computer scientist, I find recursive functions to be frightening and unnatural. I'd appreciate if anyone can tell me the pythonic idiom to accomplish this. Thanks for your help,
40
3002
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
4
4217
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real implementation usage of these concepts? Where these concepts will be used. Please provide some illustration (real-time), so that it can be easily comprehended. Thanks, wittyGuy
5
2857
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function invisible() { alert("invisible() called"); } }
6
1693
by: learning | last post by:
I am trying to learn STL but got stuck in for_each(). What I intend to do in the following is to make a list with each element as a string. add the string elements to the list until it has 10 elements. each element is the same 1234567890. then I want to use for_each to iterate the list, and for each to call apply() whihc is create a filen with name the same as string al; and within each file, it contains the content of the list...
28
16470
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a subset of C/C++, thus will have the same syntax as C/C++. Somehow the interpreted code must be able to store generic function pointers because there is no way for the interpreter to know every possible function signature in advance. I was...
9
7357
by: David Trimboli | last post by:
On a couple of my pages http://www.trimboli.name/rune/goblinmagic.html http://www.trimboli.name/rune/labyrinth.html I've got a number of definition lists concerning "Points Spent" and "Points Earned." A style sheet http://www.trimboli.name/rune/section.css makes the list definitions inline and adds "; " after each one (under "Encounter costs and mechanics" in the style sheet).
1
2491
Dormilich
by: Dormilich | last post by:
Hi, I'm running into a problem with the <xsl:processing-instruction> element (Sablotron). purpose: I generate a xhtml fragment from a xml file via xslt. This is printed (echo) to the output (i.e. all xml/xsl processing is done on the server). This is fine, as long as the output contains only xhtml. problem: one special page uses AJAX, where the result is taken from a MySQL database. To make it easier writing copy, I (sucessfully) use...
1
6474
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements = $parent->childNodes; need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error need help to recode line to match my hosting server. im using php 5 i have attached the common_method file
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.