Gold mine of Visual C++ tricks!
How to Delete Pointers in Vector or Map in Single Line?
![]()
If you have two or three STL containers which holds pointers in your class as members, then I’m sure that its destructor will be the worst readable one. For deallocating STL containers, we have to iterate through each container by for loop, then delete it. But is there any single line function call to delete all pointers in vector or map, just like chopping the top of a tray of eggs at once?

Picture Courtesy – elitalice.com
![]()
You can use for_each() and functors to achieve this. Check out the code snippet.
1) How to delete Vector of pointers in single line
// Necessary headers.
#include "functional"
#include "vector"
#include "algorithm"
using namespace std;
// Functor for deleting pointers in vector.
template<class T>
struct DeleteVectorFntor
{
// Overloaded () operator.
// This will be called by for_each() function.
bool operator()(T x) const
{
// Delete pointer.
delete x;
return true;
}
};
// Test Function.
void TestVectorDeletion()
{
// Add 10 string to vector.
vector<CString*> StringVector;
for( int Index = 0; Index < 10; ++Index )
{
StringVector.push_back( new CString("Hello"));
}
// Now delete the vector in a single line.
for_each( StringVector.begin(),
StringVector.end(),
DeleteVectorFntor<CString*>());
}
1) How to delete Map of pointers in single line
// Necessary headers.
#include "functional"
#include "map"
#include "algorithm"
using namespace std;
// Functor for deleting pointers in map.
template<class A, class B>
struct DeleteMapFntor
{
// Overloaded () operator.
// This will be called by for_each() function.
bool operator()(pair<A,B> x) const
{
// Assuming the second item of map is to be
// deleted. Change as you wish.
delete x.second;
return true;
}
};
// Test function.
void TestMapDeletion()
{
// Add 10 string to map.
map<int,CString*> StringMap;
for( int Idx = 0; Idx < 10; ++Idx )
{
StringMap[Idx] = new CString("Hello");
}
// Now delete the map in a single line.
for_each( StringMap.begin(),
StringMap.end(),
DeleteMapFntor<int,CString*>());
}
![]()
STL is really a powerful toolkit. Isn’t it?
![]()
Targeted Audience – Intermediate.
| Print article | This entry was posted by Jijo Raj on March 2, 2009 at 7:11 pm, and is filed under C++, Codeproject. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 1 year ago
I don’t know STL. I’m using C++ in general and MFC in specific.
I have problem about memory when deleting one/all/some element from a list/vector. Using the same declaration as above:
vector StringVector;
I have tried many different way to initialize the list, delete/clear items, but the memory usage of the whole program (as seen by Task Manager) keep increasing. The object(s) in the list are simply ‘de-linked’ but still remain in the memory.
I think your code above will suffer from the same issue. Any idea about this symptom?
about 1 year ago
Hi Torin,
Are you sure that you called the delete for each pointer before clearing the list? Or else it will surely leak. In the sample, operator() is calling delete. Well, as a fact MFC itself have a lot of leaks. It will be nice to inspect your code by using NuMega BoundsChecker or Rational Purify or by using some free memory detecting tool like this – http://www.codeproject.com/KB/debug/Memory_leak_finder.aspx
Regards,
Jijo.
about 1 year ago
The headers you’ve included are all standard headers. The conventional way to include those is to use angle brackets.
You’re kind of suggesting that putting raw pointers in a container that need to be managed is OK. I beg to differ. That’s what smart pointers are for (i.e. tr1::shared_ptr) — It certainly doesn’t make much sense for string objects but that’s okay becuase it’s just an example.
The value_type of a map is pair<const key_type, mapped_type> but in your functor you take a pair<key_type, mapped_type> by value which includes copying the key object. Since your functor is generic and the key class might be anything you should accept the map’s value by reference-to-const.
- P
about 1 year ago
Hi Pizer,
Thanks for going through my post and spend time to niggle out the problems.
>> The headers you
about 1 year ago
[code]
// move template decl to the method itself.
class DeleteContainerElement
{
public:
template void operator()(T* pT){ delete pT; }
};
std::for_each( c.begin(), c.end(), DeleteContainerElement() );
[/code]
With this version you can delete container with raw pointers of any type.