473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

for-each and other objects

I have the following loop (names changed to protect the innocent):

for(MyContainer::const_iterator i=instance.begin();i !=
instance.end();++i)
{
hisContainerInstance.add(i->name());
}

so I am adding the names in the instance of MyContainer to hisContainer
instance, essentially a push_back operation.

Question: Can this be implemented as a for-each loop?

I can get as far as calling a member function of MyClass in the for-each:

for_each(instance.begin(),instance.end(),mem_fun_r ef(&MyContainer::name))

but then can I call a function recurrently on *that* result?
e.g I'd like to:

for_each(instance.begin(),instance.end(),
hisContainerInstance.add(mem_fun_ref(&MyContainer: :name)))
cheers

shaun
Jul 23 '05 #1
3 1140

"shaun" <sh*******@cern.ch> wrote in message
news:sh*****************************@sunnews.cern. ch...
|I have the following loop (names changed to protect the innocent):
|
| for(MyContainer::const_iterator i=instance.begin();i !=
| instance.end();++i)
| {
| hisContainerInstance.add(i->name());
| }
|
| so I am adding the names in the instance of MyContainer to hisContainer
| instance, essentially a push_back operation.
|
| Question: Can this be implemented as a for-each loop?
|
| I can get as far as calling a member function of MyClass in the for-each:
|
| for_each(instance.begin(),instance.end(),mem_fun_r ef(&MyContainer::name))
|
| but then can I call a function recurrently on *that* result?
| e.g I'd like to:
|
| for_each(instance.begin(),instance.end(),
| hisContainerInstance.add(mem_fun_ref(&MyContainer: :name)))

I have never tried to use std::for_each in such a way, but tell
me, what problems does your for loop example present that you
would like to use std::for_each instead ?

Do you have access to the internals of 'hisContainerInstance' ?
I mean, can you modify it if need be ?

Cheers,
Chris Val
Jul 23 '05 #2
shaun wrote:
I have the following loop (names changed to protect the innocent):

for(MyContainer::const_iterator i=instance.begin();i !=
instance.end();++i)
{
hisContainerInstance.add(i->name());
}

so I am adding the names in the instance of MyContainer to
hisContainer instance, essentially a push_back operation.

Question: Can this be implemented as a for-each loop?

I can get as far as calling a member function of MyClass in the
for-each:

for_each(instance.begin(),instance.end(),mem_fun_r ef(&MyContainer::name))


See www.boost.org then:

#include <boost/bind.hpp>

typdedef ??? tOther; // type of hisContainerInstance
typdedef MyContainer::value_type tInst;

...

for_each( instance.begin()
, instance.end()
, boost::bind( &tOther::add
, &hisContainerInstance
, boost::bind( &tInst::Name, _1 )
)
);

Jeff Flinn
Jul 23 '05 #3
In message <sh*****************************@sunnews.cern.ch >, shaun
<sh*******@cern.ch> writes
I have the following loop (names changed to protect the innocent):

for(MyContainer::const_iterator i=instance.begin();i !=
instance.end();++i)
{
hisContainerInstance.add(i->name());
}

so I am adding the names in the instance of MyContainer to hisContainer
instance, essentially a push_back operation.

Question: Can this be implemented as a for-each loop?

I can get as far as calling a member function of MyClass in the for-each:

for_each(instance.begin(),instance.end(),mem_fun_ ref(&MyContainer::name))

but then can I call a function recurrently on *that* result?
e.g I'd like to:

for_each(instance.begin(),instance.end(),
hisContainerInstance.add(mem_fun_ref(&MyContainer: :name)))

How about this instead?

transform(instance.begin(), instance.end(),
back_inserter(hisContainerInstance),
mem_fun_ref(&MyContainer::name));

If hisContainerInstance doesn't have push_back() by that name, you may
need to write your own equivalents of back_inserter and
back_insert_iterator, but they're quite simple.

--
Richard Herring
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xerxes | last post by:
Hi, is there any script to authenticate an email address entered in a form field? I used the php mail() function, using the following (where my email field on the form is called "email"): ...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
0
by: Verizon | last post by:
Has anybody ever heard of support for the book: "Secure PHP Development" by: Mohammed J. Kabir I'm trying to run one of his PHP solutions called "Web Forms Manager" I haven't been able to...
0
by: panic | last post by:
If you are not familiar with the palm DB apps I am refering to, they are applications that allow you to graphically create a db by adding fields(columns) and then creats a form so you can enter the...
33
by: Frank | last post by:
What is the best IDE for developing PHP applications? What do you use and why? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.