Click here to Skip to main content
11,636,300 members (76,726 online)
Click here to Skip to main content
Add your own
alternative version

A Functor that deletes Pointers from STL Sequence Containers

, 21 Mar 2004 BSD 55.4K 618 16
A policy based deletion functor that can be used with for_each function.
#include <vector>
#include <algorithm>
#include "FreePtr.h"
using namespace std;
using namespace FreePtr;

class C
    char* data_;
            data_ = new char[512];
            delete[] data_;

int main ()
    vector <C*> objects;
    for (int i = 0; i < 20; ++i)
        objects.push_back(new C());
    for_each(objects.begin(), objects.end(), free_ptr<C>());

    for (int i = 0; i < 20; ++i)
        objects.push_back(new C[10]);
    for_each(objects.begin(), objects.end(), free_ptr<C, DeleteArray, ZeroPtr>());

    for (int i = 0; i < 20; ++i)
    for_each(objects.begin(), objects.end(), free_ptr<C, Free>());

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Nemanja Trifunovic
Software Developer (Senior) SAP
United States United States
Born in Kragujevac, Serbia. Now lives in Boston area with his wife and daughters.

Wrote his first program at the age of 13 on a Sinclair Spectrum, became a professional software developer after he graduated.

Very passionate about programming and software development in general.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 22 Mar 2004
Article Copyright 2004 by Nemanja Trifunovic
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid