Upload
bezdomnimn
View
248
Download
0
Embed Size (px)
Citation preview
8/7/2019 WINPRO MFC Collection class
1/14
Windows programming
MFC collection class
Amarsanaa .G
School of Information Technology
National University of Mongolia
8/7/2019 WINPRO MFC Collection class
2/14
MFC Collection class
A collection class is used to create objectsthat are capable of storing other objects.
Using a collection allows the programmer tohold, process, or store groups of class objectsor variables of certain standard types.
Three basic types of collection class: CArray, CList, CMap
Each is available in template and non-template version.
#include
8/7/2019 WINPRO MFC Collection class
3/14
CArray class
A safe array and enables you to store and accessdata using subscript, or [ ] operator.
CArray consumes memory and time when growing or
attempting to store in the middle. If time is a factor then use a simple array instead
which does not grow dynamically.
Template class definition in header file:
template < class TYPE, class ARG_TYPE =const TYPE& > class CArray : publicCObject
8/7/2019 WINPRO MFC Collection class
4/14
CList class
A linked list.
Supports ordered lists of nonunique objects
accessible sequentially or by value.
template< class TYPE, classARG_TYPE = const TYPE& > classCList : public CObject
8/7/2019 WINPRO MFC Collection class
5/14
CMap class
A hash table.
A dictionary collection class that maps unique
keys to values. Key is converted into a hash value for use
as index to an array of collection items.
template< class KEY, class ARG_KEY,class VALUE, class ARG_VALUE>class CMap : public CObject
8/7/2019 WINPRO MFC Collection class
6/14
Collection Shape Features
Article on msdn.microsoft.com: Recommendations for Choosing a Collection Class
http://msdn.microsoft.com/en-us/library/y1z022s1(VS.80).aspx, Mar 2009
8/7/2019 WINPRO MFC Collection class
7/14
Types of CArray
The array collection classes are named forthe objects that they hold and are: CByteArray - BYTE values CWordArray - WORD values CDWordArray - DWORD values CPtrArray - void pointer values CObArray - class object pointers
CStringArray - CString objects CUIntArray - UINT values
These classes are non-template CByteArray class definition in
class CByteArray : public CObject
8/7/2019 WINPRO MFC Collection class
8/14
Using CArray class
The member function 'SetAtGrow' stores anitem in the CArray at the specified position or
index. If the CArray is not large enough tohold, it expands to accommodate the newitem.
The member function 'SetAt' does the samebut does not increase the CArray sizeautomatically.
The member function 'GetSize' gives thenumber of items stored in the CArray.
8/7/2019 WINPRO MFC Collection class
9/14
CArray membersAccessingtheArrayBounds
GetSize() Gets the number of elements in this array.GetUpperBound() Returns the largest valid index.SetSize() Sets the number of elements to be contained in this array.
OperationsFreeExtra() Frees all unused memory above the current upper bound.RemoveAll() Removes all the elements from this array.
ElementAccessGetAt() Returns the value at a given index.SetAt() Sets the value for a given index; array not allowed to grow.ElementAt() Returns a temporary reference to the element pointerwithin the array.
GrowingtheArraySetAtGrow() Sets the value for a given index; grows the array ifnecessary.Add() Adds an element to the end of the array; grows the array ifnecessary.
Insertion/RemovalInsertAt() Inserts an element (or all the elements in another array) ata specified index.RemoveAt() Removes an element at a specific index.
Operatorsoperator [] Sets or gets the element at the specified index.
8/7/2019 WINPRO MFC Collection class
10/14
Example of CArray
#include // for using MFC class CString in a console type
app.#include #include // for use of any MFC collection classesint main(){
CArray rgSports;// The first parameter represents the type ofobject stored inside the list, the next// parameter is the type of object passed as anargument for CArray functions
// Define the objects.CString szBaseball("Baseball - NY Yankees");CString szSoccer("Soccer - NY Giants");CString szCricket("Cricket - South Africa");CString szHockey("Hockey - Korea");
CString szBasketball("Baseball - Celtics");
8/7/2019 WINPRO MFC Collection class
11/14
Example of CArray
// Add five teams to the rgSports array. The firstparameter becomes the index and
// the second CString name , the reference to it willbe passed.
rgSports.SetAtGrow(0, szBaseball);rgSports.SetAtGrow(1, szSoccer);rgSports.SetAtGrow(0, szCricket);rgSports.SetAtGrow(0, szHockey);rgSports.SetAtGrow(0, szBasketball);
// Display the contents of the Sports array.cout
8/7/2019 WINPRO MFC Collection class
12/14
MemoryType of array CString
Baseball - NY Yankees
Soccer - NY Giants
Cricket - South Africa
Hockey- Korea
Baseball - Celtics
rgSports[0]
rgSports[1]
rgSports[2]
rgSports[3]
rgSports[4]
Memory type: CString object
8/7/2019 WINPRO MFC Collection class
13/14
Output of Example
8/7/2019 WINPRO MFC Collection class
14/14
?