C++ Generic code for deleting pointer value in Map and vector of pointers
<p>I have some generic code for deleting pointers within a vector or a value of a Map.</p>

<p>Is there a better way of doing this (without using shared_ptrs or any o fthe tr1 extensions )?</p>

<p>Also is the code correct?</p>

<p>Here is my code:</p>

<p>I have a namespace</p>

<pre><code>#ifndef CONTAINERDELETE_H
#define CONTAINERDELETE_H

#include &lt;functional&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;


namspace ContainerDelete{

  template&lt;class A, class B&gt;
    struct DeleteMap
    {
      bool operator()( pair&lt;A,B&gt; &amp;x) const
      {

        delete x.second;
        return true;
      }
    };

   template&lt;class T&gt;
    struct DeleteVector
    {
      bool operator()(T &amp;x) const
        { 
          delete x;
          return true;
        }
    };
 }
 #endif
</code></pre>

<p>I would then use this namespace in some bit of code to delete a map or vector.</p>

<p>Test Map deletion.</p>

<pre><code>#include "ContainerDelete.h"

using namespace std;

// Test function.
void TestMapDeletion()
{
   // Add 10 string to map.
   map&lt;int,B*&gt; testMap;
   for( int Idx = 0; Idx &lt; 10; ++Idx )
   {
     testMap[Idx] = new B();
   }

   // Now delete the map in a single  line.
   for_each( testMap.begin(),
             testMap.end(),
             ContainerDelete::DeleteMap&lt;int,B*&gt;());
}
</code></pre>

<p>Test Vector Deletion</p>

<pre><code>// Test Function.
void TestVectorDeletion()
{
  // Add 10 string to vector.
  vector&lt;B*&gt; testVector;
  for( int Index = 0; Index &lt; 10; ++Index )
  {
    testVector.push_back( new B());
  }

  // Now delete the vector in a single  line.
  for_each( testVector.begin(),
            testVector.end(),
            ContainerDelete::DeleteVector&lt;B*&gt;());
} 
</code></pre>

<p>Thanks,</p>

<p>Mike</p>
