Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:11:16+00:00 2026-05-27T01:11:16+00:00

I have some generic code for deleting pointers within a vector or a value

  • 0

I have some generic code for deleting pointers within a vector or a value of a Map.

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

Also is the code correct?

Here is my code:

I have a namespace

#ifndef CONTAINERDELETE_H
#define CONTAINERDELETE_H

#include <functional>
#include <map>
#include <vector>
#include <algorithm>


namspace ContainerDelete{

  template<class A, class B>
    struct DeleteMap
    {
      bool operator()( pair<A,B> &x) const
      {

        delete x.second;
        return true;
      }
    };

   template<class T>
    struct DeleteVector
    {
      bool operator()(T &x) const
        { 
          delete x;
          return true;
        }
    };
 }
 #endif

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

Test Map deletion.

#include "ContainerDelete.h"

using namespace std;

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

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

Test Vector Deletion

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

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

Thanks,

Mike

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:11:17+00:00Added an answer on May 27, 2026 at 1:11 am

    Better would be if reduce the genericity as:

    struct DeleteVector
    {
        template<class T>  //use the template here!
        void operator()(T &x) const
        { 
          delete x;
        }
    };
    

    if you do so, then you could simply write this:

    for_each(testVector.begin(),
             testVector.end(),
             ContainerDelete::DeleteVector());
    

    No need to pass type argument when you use DeleteVector, for it is not a class template anymore!

    Similarly, you can implement DeleteMap functor.

    You should also rename DeleteVector to DeleteT, and DeleteMap to DeletePairSecond, as both of these can be used more generically. For example, DeleteT can be used even with std::list, or even with arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a better way to this. I have a chunk of code
Here's the situation: We have some generic graphics code that we use for one
I have to work on some code that's using generic lists to store a
I have a generic list. Some elements of this list belong to a parent
I have some fairly generic code which uses preprocessor macros to add a certain
I have some code that has generic references in it and my IBM RAD
I'm trying to write some reasonably generic networking code. I have several kinds of
I have been developing some Django app and there's some duplicated code for different
I have some code that assigns an object to a generic id variable and
I have some tricky generic type problem involving reflection. Here's the code. public @interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.