Collections
The simplest collections to use are CStringList and CStringArray. A list of other things, such as a list or array of integers, can easily be created using template classes such as CList and CArray.
CStringList
The following is a simple sample of using a CStringList. The first thing to do is to define a variable, as in:
CStringList StringList;
Then add entries, as in:
StringList.AddTail("A string");
Then iterating through the list could be as easy as in the following:
CString String; POSITION Position = StringList.GetHeadPosition(); while (Position) String = StringList.GetNext(Position);
Notice that there is not any clean-up necessary beyond the default destructor. When the CStringList is deleted, all strings are also deleted. This is different when a collection contains pointers, because then the data pointed to must be deleted prior to deleteing the collection.
CList
The following is a simple sample of using a CList. This sample is kept especially simple by making it a list of integers. The first thing to do is to make a typedef, as in:
typedef CListCListType;
Next, use the typedef as in the following:
CListType List;
Then add entries as in (where "i" is an "int"):
List.AddTail(i);
Finally, iterate through the list as in the following:
int i; POSITION Position = List.GetHeadPosition(); while (Position) i = List.GetNext(Position);
Notice that since the list contents are not pointers, as in the case of a CStringList, there is not any clean-up necessary.
CObList
If it is necessary to store a class or struct, a CObList can be used. The following is a simple sample of using a CObList:
class CStringObject : public CObject { DECLARE_DYNAMIC(CStringObject) public: CString m_String; };
IMPLEMENT_DYNAMIC(CStringObject, CObject)
CStringObject *pStringObject; POSITION ObjectListPos; CObList ObjectList; pStringObject = new CStringObject; pStringObject->m_String = "First Object"; ObjectListPos = ObjectList.AddTail(pStringObject); pStringObject = new CStringObject; pStringObject->m_String = "Last Object"; ObjectListPos = ObjectList.AddTail(pStringObject);
ObjectListPos = ObjectList.GetHeadPosition(); while (ObjectListPos!=NULL) { pStringObject = (CStringObject *) ObjectList.GetNext(ObjectListPos); TRACE1("%s\n", pStringObject->m_String); delete pStringObject; } ObjectList.RemoveAll();
CTypedPtrMap
The following is an example of using CTypedPtrMap.
class CRecord { public: int m_Index; };
typedef CTypedPtrMap <CMapStringToPtr, CString, CRecord *> CRecords;
CRecords m_Records;
CString String; CRecord *pRecord; m_Records.SetAt(String, pRecord);
Position = m_Records.GetStartPosition(); while (Position!=NULL) { m_Records.GetNextAssoc(Position, String, pRecord); TRACE("%s %i\n", String, pRecord->m_Index); delete pRecord; } m_Records.RemoveAll();
CMap
The following is an example of using CMap.
typedef CMapCIntegerMap;
CIntegerMap m_IntegerMap;
CString String; int Index; m_IntegerMap.SetAt(Index, CString(String));
Position = m_IntegerMap.GetStartPosition(); while (Position!=NULL) { m_IntegerMap.GetNextAssoc(Position, Index, String); m_Outputfile.WriteString(String); String.Format(" %i\n", Index); m_Outputfile.WriteString(String); } m_IntegerMap.RemoveAll();
Note: the default hash key for CMap is a DWORD, so unless you override CMap's HashKey function, the hash key cannot be more than four bytes. Microsoft Knowledge Base article Q158541 ("PRB: C2440 Error When Using CMap and User Defined Key Type") provides a sample of CMap with a HashKey override to allow use of a user-defined class for a key.
See my Visual C++ Programmer Stuff page for more C++ stuff.
'Windows > MFC' 카테고리의 다른 글
CDialog 에 Toolbar 를 추가하는 방법. (0) | 2013.10.02 |
---|---|
CListCtrl 사용법 (0) | 2013.10.02 |
CListCtrl 에서 선택한 아이템 삭제하기 (0) | 2013.10.02 |
CFileException CArchiveException (0) | 2013.10.02 |
CStringList Serialize Code (0) | 2013.10.02 |