共用方式為


CObList::RemoveAt

Removes the specified element from this list.

void RemoveAt( 
   POSITION position  
);

Parameters

  • position
    The position of the element to be removed from the list.

Remarks

When you remove an element from a CObList, you remove the object pointer from the list. It is your responsibility to delete the objects themselves.

You must ensure that your POSITION value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.

The following table shows other member functions that are similar to CObList::RemoveAt.

Class

Member Function

CPtrList

void RemoveAt( POSITION position );

CStringList

void RemoveAt( POSITION position );

Example

Be careful when removing an element during a list iteration. The following example shows a removal technique that guarantees a valid POSITION value for GetNext.

See CObList::CObList for a listing of the CAge class.

CObList list;
POSITION pos1, pos2;
CObject* pa;

list.AddHead(new CAge(21));
list.AddHead(new CAge(40));
list.AddHead(new CAge(65)); // List now contains (65 40, 21). 
for (pos1 = list.GetHeadPosition(); (pos2 = pos1) != NULL;)
{
    if (*(CAge*) list.GetNext(pos1) == CAge(40))
    {
        pa = list.GetAt(pos2); // Save the old pointer for 
                               //deletion.
        list.RemoveAt(pos2);
        delete pa; // Deletion avoids memory leak.
    }
}
#ifdef _DEBUG
   afxDump.SetDepth(1);
   afxDump << _T("RemoveAt example: ") << &list << _T("\n");
#endif      

The results from this program are as follows:

RemoveAt example: A CObList with 2 elements

a CAge at $4C1E 65

a CAge at $4B22 21

Requirements

Header: afxcoll.h

See Also

Reference

CObList Class

Hierarchy Chart