0% found this document useful (0 votes)
49 views14 pages

WINPRO MFC Collection Class

The document discusses MFC collection classes in C++, specifically CArray, CList, and CMap. CArray allows storing and accessing objects using a subscript operator and dynamically resizes memory. CList implements a linked list for ordered but non-unique objects. CMap provides a hash table that maps unique keys to values. Examples demonstrate adding objects to a CArray and iterating through its contents.

Uploaded by

bezdomni_mn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views14 pages

WINPRO MFC Collection Class

The document discusses MFC collection classes in C++, specifically CArray, CList, and CMap. CArray allows storing and accessing objects using a subscript operator and dynamically resizes memory. CList implements a linked list for ordered but non-unique objects. CMap provides a hash table that maps unique keys to values. Examples demonstrate adding objects to a CArray and iterating through its contents.

Uploaded by

bezdomni_mn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Windows programming

MFC collection class

Amarsanaa .G
School of Information Technology
National University of Mongolia
MFC Collection class
 A collection class is used to create objects
that are capable of storing other objects.
 Using a collection allows the programmer to
hold, process, or store groups of class objects
or variables of certain standard types.
 Three basic types of collection class:
 CArray, CList, CMap
 Each is available in template and non-
template version.
 #include <afxtempl.h>
CArray class
 A safe array and enables you to store and access
data 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 <afxtempl.h> header file:

template < class TYPE, class ARG_TYPE =


const TYPE& >  class CArray :  public
CObject
CList class
 A linked list.
 Supports ordered lists of nonunique objects
accessible sequentially or by value.

template< class TYPE, class


ARG_TYPE = const TYPE& >  class
CList : public CObject
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
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
Types of CArray
 The array collection classes are named for the 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 <afxcoll.h>
class CByteArray : public CObject
Using CArray class
 The member function 'SetAtGrow' stores an
item in the CArray at the specified position or
index. If the CArray is not large enough to
hold, it expands to accommodate the new
item.
 The member function 'SetAt' does the same
but does not increase the CArray size
automatically.
 The member function 'GetSize' gives the
number of items stored in the CArray.
CArray members
Accessing the Array Bounds
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.
Operations
FreeExtra() Frees all unused memory above the current upper bound.
RemoveAll() Removes all the elements from this array.
Element Access
GetAt() 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 pointer within
the array.
Growing the Array
SetAtGrow() Sets the value for a given index; grows the array if necessary.
Add() Adds an element to the end of the array; grows the array if necessary.
Insertion/Removal
InsertAt() Inserts an element (or all the elements in another array) at a
specified index.
RemoveAt() Removes an element at a specific index.
Operators
operator [] Sets or gets the element at the specified index.
Example of CArray
#include <afx.h>
// for using MFC class CString in a console type
app.
#include <iostream.h>
#include <afxtempl.h>
// for use of any MFC collection classes
int main()
{
CArray<CString, CString&> rgSports;
// The first parameter represents the type of
object stored inside the list, the next
// parameter is the type of object passed as an
argument 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");
Example of CArray
// Add five teams to the rgSports array. The first
parameter becomes the index and
// the second CString name , the reference to it will
be 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 << " The following are the major teams in
respective sports:" << endl;
int nSports = rgSports.GetSize();
for( int nSportsIndex = 0; nSportsIndex < nSports;
nSportsIndex++ )
{
cout << "\t" << rgSports[nSportsIndex] << endl;
}
// Remove the the items stored in the rgSports array.
rgSports.RemoveAll();
return EXIT_SUCCESS;
}
Memory
Type of array CString
rgSports[0] Baseball - NY Yankees

rgSports[1] Soccer - NY Giants

rgSports[2] Cricket - South Africa

rgSports[3] Hockey - Korea

rgSports[4] Baseball - Celtics

Memory type: CString object


Output of Example
?

You might also like