for-each and other objects

S

shaun

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)))


cheers

shaun
 
C

Chris \( Val \)

|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)))

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
 
J

Jeff Flinn

shaun said:
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))

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
 
R

Richard Herring

shaun said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top