Download MFC Collection Classes: Arrays, Linked Lists, and Maps and more Study notes Object Oriented Programming in PDF only on Docsity!
MFC Collection Classes
References
This material is drawn from “Programming
Windows with MFC”, 2nd Edition, Jeff Prosise,
Microsoft Press, 1999, Chapter 5
This presentation is adapted from one
presented by Pei Lin and Huan-Tang Yang,
Summer 2001
Goals
• Introduce MFC collection classes.
• Describe common usage
• List member functions to serve as a quick
reference.
Motivation
• To gain extra power over data in your programs
- (^) bounds-checking and handling
- (^) Change size dynamically – provides run-time flexibility
• To simplify many operations involving use of
complex data structures
• Compare to STL classes in MFC applications
- (^) VC++ Ver 6.0 is not thread-safe. Much of MFC is
multi-threaded
- (^) using MFC collection classes will avoid your application
linking to two separate class libraries.
Classification – by type MFC collection classes not based on templates - Defined in Afxcoll.h Arrays Lists Maps
CByteArray (BYTEs) CObList CMapWordToPtr
CWordArray (WORDs) CPtrList CMapPtrToWord
CDWordArray (DWORD
s)
CStringList CMapPtrToPtr
CUIntArray (UINTs) CMapWordToOb
CStringArray (CStrings
CMapStringToOb
CPtrArray (void
pointers)
CMapStringToPtr
CObArray (CObject
ptrs)
CMapStringToString
MFC Array Classes
(A checkmark indicates that function can grow dynamically.)
Member Functions of the Array Classes:
Add( ) Appends a value to the end of the array, increasing the size of the array
as needed.
- (^) ElementAt( ) Gets a reference to an array element’s pointer.
- (^) FreeExtra( ) Releases unused array memory.
- (^) GetAt( ) Gets the value at the specified array index.
- (^) GetSize( ) Gets the number of elements in the array.
- (^) GetUpperBound( ) Gets the array's upper bound, which is the highest valid index at which a value can be stored.
InsertAt( ) Inserts a value at the specified index, shifting existing elements upward
as necessary to accommodate the insert.
How much it will grow?
- (^) You can use setSize method to specify how big
you want the array grow each time.
- (^) If you don’t, MFC picks one for you using a
simple formula based on the array size. The
larger the array, the larger the grow size.
- (^) The default grow size is 4 items.
Member functions
(^) SetAt( int nIndex, ARG_TYPE newElement ) (^) GetAt( int nIndex ) (^) GetSize() (^) GetUpperBound() (^) RemoveAt( int nIndex, int nCount = 1 ) (^) RemoveAll( )
DynamicArraySizing
Functions
• SetSize()
• SetAtGrow()
• Add ()
• InsertAt ()
MFC List Classes
• Doubly linked lists for fast item insertion and
removal
• POSITION type:
A pointer to a CNode data structure (a list
item)
Member Functions of List
Classes
- (^) GetNext( ) When iterating over list, gets next node.
- (^) GetPrev( ) When iterating over list, gets previous node.
- (^) GetTail( ) Gets the list's tail node.
- (^) GetTailPosition( ) Gets the tail node's position.
- (^) InsertAfter( ) Inserts a new node after the specified position.
- (^) InsertBefore( ) Inserts a new node before the specified position.
- (^) IsEmpty( ) Returns TRUE if list is empty and returns FALSE otherwise.
- (^) RemoveAll( ) Removes all of a list's nodes.
- (^) RemoveAt( ) Removes a single node from a list.
- (^) RemoveHead( ) Removes the list's head node.
- (^) RemoveTail( ) Removes the list's tail node.
- (^) SetAt( ) Sets the node at the specified position.
Caution
In Clist:
- (^) If you use Find function, you should overload
operator ==
- (^) Or you can overload template function
"CompareElements“
- (^) CPoint and CString have overloaded operator
Member Functions of map
Classes
- (^) GetCount( ) Gets the number of map elements
- (^) GetNextAssoc( ) When iterating over the map, gets the next element
- (^) GetStartPosition( ) Gets the first element's position
- (^) IsEmpty( ) Returns TRUE if the map is empty and returns FALSE otherwise
- (^) Lookup( ) Finds the value associated with a key
- (^) RemoveAll( ) Removes all of the map's elements
- (^) RemoveKey( ) Removes an element from map
- (^) SetAt( ) Adds map element or replaces element with matching key