Title: Programming Windows with MFC
1Programming Windows with MFC
2Chapter 5
- The MFC Collection Classes
3The Standard Template Library
- Convenient implementations of arrays, linked
lists, maps, and other containers - A container in the language of STL
- An object that stores collections of data
4The MFC Collection Classes
- MFC collection classes
- own implementations of arrays, linked lists, and
maps in a family of classes - Never have to write a linked list
5Arrays
6Arrays
int array10 for (int i0 ilt10 i)
arrayi i 1
7Arrays
class CArray int Get (int nIndex)
assert (nIndex gt 0 nIndex lt m_nSize)
return m_pDatanIndex void Set (int
nIndex, int nVal) assert (nIndex gt 0
nIndex lt m_nSize) m_pDatanIndex nVal
CArray array (10) for (int i0 ilt10 i)
array.Set (i, i 1) // Asserts when i 10.
8The MFC Array Classes
- CArray class
- A template class
- Create type-safe arrays
- Defined in the header file Afxtempl.h
- Nontemplatized array classes
- Designed to hold a particular type of data
- defined in Afxcoll.h
9Type-Specific MFC Array Classes
10Usages
- SetSize
- InsertAt
- RemoveAt
- RemoveAll
- CUIntArray array
- array.SetSize (10)
- for (int i0 ilt10 i)
- arrayi i 1
11Dynamic Array Sizing
- SetSize
- SetAtGrow
- Add
- InsertAt
- Append
- Copy
12CMemoryExceptions
- try
- CUIntArray array
- // Might throw a CMemoryException.
- array.SetSize (1000)
-
-
- catch (CMemoryException e)
-
- AfxMessageBox (_T ("Error
- Insufficient memory"))
- // Delete the exception object.
- e-gtDelete ()
13Creating Type-Safe Array Classes with CArray
- Your own MFCs CArray class
CArrayltCPoint, CPointgt array // Populate the
array, growing it as needed. for (int i0 ilt10
i) array.SetAtGrow (i, CPoint (i10, 0))
// Enumerate the items in the array. int
nCount array.GetSize () for (i0 iltnCount
i) CPoint point arrayi TRACE (_T
("xd, yd\n"), point.x, point.y)
14Lists
15Linked list
- Support fast item insertion and removal
- A collection of items that contain pointers to
other items
item pItem GetHead () while (pItem ! NULL)
pItem pItem-gtpNextItem item pItem
GetTail () while (pItem ! NULL) pItem
pItem-gtpPrevItem
16Type-Specific MFC List Classes
17Lists
- AddHead
- RemoveHead/RemoveTail/RemoveAll
- GetNex/GetPrev
- GetHeadPosition/GetTailPosition
- Find
18Creating Type-Safe List Classes with CList
- CListltCPoint, CPointgt list
- // Populate the list.
- for (int i0 ilt10 i)
- list.AddTail (CPoint (i10, 0))
- // Enumerate the items in the list.
- POSITION pos list.GetHeadPosition ()
- while (pos ! NULL)
- CPoint point list.GetNext (pos)
- TRACE (_T ("xd, yd\n"), point.x, point.y)
-
19Maps
20Type-Specific MFC Map Classes
21CMapStringToString
- CMapStringToString map
- map_T("Sunday") _T("Dimanche")
- map_T("Monday") _T("Lundi")
- map_T("Tuesday") _T("Mardi")
- map_T("Wednesday") _T("Mercredi")
- map_T("Thursday") _T("Jeudi")
- map_T("Friday") _T("Vendredi")
- map_T("Saturday") _T("Samedi")
- CString string
- if (map.Lookup (_T ("Thursday"), string))
- TRACE (_T ("Thursday in English s in
French\n"), string)
22The Typed Pointer Classes
- A set of three template classes designed to
handle collections of pointers in a type-safe
manner
23Collection Classes for Pointers
24CObList
- CTypedPtrListltCObList, CLinegt list
- // Populate the list.
- for (int i0 ilt10 i)
- int x i 10
- CLine pLine new CLine (x, 0, x, 100)
- list.AddTail (pLine)
-
- // Enumerate the items in the list.
- POSITION pos list.GetHeadPosition ()
- while (pos ! NULL)
- CLine pLine list.GetNext (pos) // No
casting!