3 Mar
2005
3 Mar
'05
2:28 p.m.
"Peter Dimov"
Johan Nilsson wrote:
[...]
std::vector
foos(10); std::for_each(foos.begin(), foos.end(), _1 = shared_ptr<Foo>(new Foo)); Lambda way (according to the docs, I haven't tried it):
std::for_each( foos.begin(), foos.end(), _1 = bind( constructor< shared_ptr<Foo> >(), new_ptr<Foo>() ) );
(probably leaks when new throws)
Old school way:
shared_ptr<Foo> createFoo() { return shared_ptr<Foo>( new Foo ); }
std::generate( foos.begin(), foos.end(), createFoo );
here is also BOOST_FOREACH way: BOOST_FOREACH( shared_ptr<Foo>&, elem, foos ) elem.reset( new Foo ); Gennadiy