for( ContainerType::iterator itr = c.begin(); itr != c.end(); ++itr)
{
delete *itr;
}
shouldn't there be a easier more explicit way? of course there is,
using std::for_each and a simple functor we get this:
//some_header.h
struct delete_object
{
template <typename T>
void operator()(T *ptr){ delete ptr;}
};
//later in the code...
std::for_each( c.begin(), c.end(), delete_object());
ahhh, doesn't that feel nice? less to type extra clarity *and* it could actually
give you a speed gain becuase c.end() isn't recalculated oh and that it's less
error prone is just an added bonus =)


This topic is locked









