CObList::RemoveAt
void RemoveAt( POSITION position );
参数:
说明:
从列表中删除指定的元素。
当从CObList中删除元素时, 仅从列表中删除对象指针。对象本身得由自己删除。
必须保证所给的POSITION 值指向列表的一个有效的位置。若位置值无效,则MFC库(Microsoft Foundation Class Library)的调试版本将就此给出警告声明。
示例:
在列表迭代过程中删除一元素时要特别注意。下面的示例给出了能确保GetNext取到一个有效的POSITION值的删除技术。
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 << "RemoveAt example: " << &list << "\n";
#endif
该程序的结果如下:
RemoveAt example: A CObList with 2 elements
a CAge at $4C1E 65
a CAge at $4B22 21