This post copied from Effective STL.
Usually, many developer still delete object in STL container via iterating container and delete.
This method possibility makes exceptions and not effective way for STL.
From now on, use unary_function with for_each.
template <typename T>
class ContainerDeallocator : unary_function<T, void>
{
public:
void operator () (const T* ptr) const
{
delete ptr;
}
};
vector<int*> v1;
v1.resize(10);
v1[0] = new int(10);
v1[1] = new int(18);
v1[2] = new int(67);
v1[3] = new int(5);
for_each(v1.begin(), v1.end(), ContainerDeallocator<int>());
It's more pretty than using iterator.
Reference : Effective STL -Meyers-
Tuesday, January 13, 2009
Use unary_function when delete objet in STL container.
작성자: scor7910 시간 5:11 AM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment