Presentation is loading. Please wait.

Presentation is loading. Please wait.

MFC Collection Classes

Similar presentations


Presentation on theme: "MFC Collection Classes"— Presentation transcript:

1 MFC Collection Classes
Jim Fawcett CSE 791 – Advanced Windows Programming Summer 2002

2 Topics MFC collection classes – Arrays – Linked lists – maps
Description Class member functions. Demo applications

3 Goals Introduce MFC collection classes. Describe common usage
List member functions to serve as a quick reference.

4 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.

5 Classification – by type
MFC template-based collection classes - Defined in Afxtempl.h Syntax example: CArray< class TYPE, class ARG_TYPE > myArray; CTypedPtrList<class BASE_CLASS, class TYPE> myList; Content Arrays Lists Maps Type-safe objects CArray CList CMap Type-safe pointers to objects CTypedPtrArray CTypedPtrList CTypedPtrMap

6 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(DWORDs) CStringList CMapPtrToPtr CUIntArray (UINTs) CMapWordToOb CStringArray(CStrings) CMapStringToOb CPtrArray (void pointers) CMapStringToPtr CObArray(CObject ptrs) CMapStringToString

7 MFC Array Classes (A checkmark a 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.

8 Member Functions of the Array Classes
RemoveAll( ) Removes all of the array's elements. RemoveAt( ) Removes the value at the specified index. SetAt( ) Places a value at the specified index. Because this function will not increase the size of the array, the index must be currently valid. SetAtGrow( ) Places a value at the specified index, increasing the size of the array as needed. SetSize( ) Sets the array's size, which is the number of elements that the array can hold. The array still can grow dynamically beyond this size. Append( ) Appends one array to another Copy( ) Copies one array to another

9 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.

10 Member functions SetAt( int nIndex, ARG_TYPE newElement )
GetAt( int nIndex ) GetSize() GetUpperBound() RemoveAt( int nIndex, int nCount = 1 ) RemoveAll( )

11 RemoveAll ( ) If the elements are pointers,the remove functions delete elements, but they don't delete objects that the elements point to. You should do this : int nSize = array.GetSize (); for (int i=0; i<nSize; i++) delete array[i]; array.RemoveAll (); //delete objects first !!

12 DynamicArraySizing Functions
SetSize() SetAtGrow() Add () InsertAt ()

13 MFC List Classes Doubly linked lists for fast item insertion and removal POSITION type: A pointer to a CNode data structure (a list item)

14 Member Functions of list Classes
AddHead( ) Adds a node to the head of the list, making the node the new head. AddTail( ) Adds a node to the tail of the list, making the node the new tail. Find( ) Searches the list sequentially to find the given object pointer. Returns a POSITION value. FindIndex() Scans list sequentially, stopping at node indicated by the given index. Returns a POSITION value for the node. GetAt( ) Gets the node at the specified position. GetCount( ) Gets the number of nodes in the list. GetHead( ) Gets the list's head node. GetHeadPosition( ) Gets the head node's position.

15 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.

16 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 ==

17 MFC MAP Classes What is a map? How do Maps work?
A table of items keyed by other items. How do Maps work? Implemented by Hash Table so not sorted Fast searching on key type

18 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


Download ppt "MFC Collection Classes"

Similar presentations


Ads by Google