kyuseo의 게임 프로그래밍
CList 와 CMap 의 아이템 제거, 삭제(Remove At) 방법 본문
개요.. |
CList 와 CMap 는 유용한 MFC 클래스 입니다. 안전하게 아이템을 제거, 삭제 방법에 대하여 알려드립니다.
CList와 CMap 는 순차 검색을 하면서 특정 아이템을 제거하는 과정이 조금은 까다로울 수 있는데, CList 는 이전 위치를 제거하고, CMap 은 키를 삭제하면 안전하게 제거됩니다.
코드 |
- CList 의 제거 방법
int i; POSITION pos, old;
///////////////////////////////////////////////////////////////////////////// // CList 의 예 CList< CPoint, CPoint > list;
// 아이템을 추가한다. for( i=0; i<10; i++ ) { list.AddTail( CPoint(i, i) ); }
// X값이 짝수인 아이템을 제거한다. pos = list.GetHeadPosition();
while(pos!=NULL) { // 이전 위치를 저장한다. old = pos; CPoint& rPoint = list.GetNext( pos );
if( rPoint.x % 2 == 0 ) { // *** 저장된 위치로 제거한다. list.RemoveAt( old ); } } |
- CMap 의 제거 방법
///////////////////////////////////////////////////////////////////////////// // CMap 의 예 CMap< int,int,CPoint,CPoint > map;
for( i=0; i<10; i++ ) { map.SetAt( i, CPoint(i, i) ); }
// Remove the elements with even key values. int nKey; CPoint Point; pos = map.GetStartPosition(); while( pos != NULL ) { map.GetNextAssoc( pos, nKey, Point );
if( Point.x % 2 == 0 ) { // *** 그냥 키를 제거한다. map.RemoveKey( nKey ); } } |