C++删除容器数据

// free the contents of the list; erase the list

inline void ListDelete (list <void *> *pList)
{
    if (pList)
    {
        list <void *>::iterator iNext;

        for (iNext = pList->begin (); iNext != pList->end (); ++iNext)
            free (*iNext);

        pList->erase (pList->begin (), pList->end ());
    }
}

// ----------------------------------------------------------------------
//  Delete the contents of a list; Erase the list
//
template <class T>
void ListDelete (list <T> *pList)
{
    if (pList)
    {
        list <T>::iterator iNext;

        for (iNext = pList->begin (); iNext != pList->end (); )
        {
            // prefetch next position incase object destructor modifies list
            list <T>::iterator iThis = iNext++;
            delete *iThis;
        }

        pList->erase (pList->begin (), pList->end ());
    }
}

// ----------------------------------------------------------------------
//  Clear the contents of a list of pointers
//
template <class T>
void ListClear (list <T> *pList)
{
    if (pList)
    {
        list <T>::iterator iNext;

        for (iNext = pList->begin (); iNext != pList->end (); ++iNext)
            *iNext = 0;
    }
}



// ----------------------------------------------------------------------
//  Delete the contents of a map; Erase the map
//
template <class Key, class Value, class Compare> 
void MapDelete (map <Key, Value, Compare> *pMap)
{
    if (pMap)
    {
        map <Key, Value, Compare>::iterator iNext;

        for (iNext = pMap->begin (); iNext != pMap->end (); ++iNext)
            delete (*iNext).second;

        pMap->erase (pMap->begin (), pMap->end ());
    }
}

// ----------------------------------------------------------------------
//  Clears the contents of a map of pointers
//        doesn't change the key values
//
template <class Key, class Value, class Compare> 
void MapClear (map <Key, Value, Compare> *pMap)
{
    if (pMap)
    {
        map <Key, Value, Compare>::iterator iNext;

        for (iNext = pMap->begin (); iNext != pMap->end (); ++iNext)
            (*iNext).second = 0;
    }
}

// ----------------------------------------------------------------------
//  Delete the contents of a vector; Erase the vector
//
template <class Value> 
void VectorDelete (vector <Value> *pvVector)
{
    if (pvVector)
    {
        vector <Value>::iterator iNext;

        for (iNext = pvVector->begin (); iNext != pvVector->end (); ++iNext)
            delete *iNext;

        pvVector->erase (pvVector->begin (), pvVector->end ());
    }
}

// ----------------------------------------------------------------------
//  Delete the contents of a set; Erase the set
//
template <class Key, class Compare> 
void SetDelete (set <Key, Compare> *pSet)
{
    if (pSet)
    {
        set <Key, Compare>::iterator iNext;

        for (iNext = pSet->begin (); iNext != pSet->end (); ++iNext)
            delete *iNext;

        pSet->erase (pSet->begin (), pSet->end ());
    }
}

// ----------------------------------------------------------------------
//  Clears the contents of a vector of pointers
//
template <class Value> 
void VectorClear (vector <Value> *pvVector)
{
    if (pvVector)
    {
        vector <Value>::iterator iNext;

        for (iNext = pvVector->begin (); iNext != pvVector->end (); ++iNext)
            *iNext = 0;
    }
}

// ----------------------------------------------------------------------

template <class T>
class less_ptr : public binary_function<T, T, bool>
{
public:
  OS_STRUCT_CONSTRUCT (less_ptr)

  bool operator () (const T& x_, const T& y_) const
  {
    return * x_ < * y_;
  }
};

// ----------------------------------------------------------------------

template <class T>
class unary_predicate : public unary_function<T,bool>
{
    public:

        unary_predicate(){}
        virtual bool operator () ( T argument )
        {
            return false;
        }
};
原文地址:https://www.cnblogs.com/lenmom/p/9208315.html