MFC COLLECTION CLASSES Trần Anh Tuấn A

MFC COLLECTION CLASSES
Trần Anh Tuấn A
CONTENTS ( MFC COLLECTION CLASSES )
Array
List
Map
• Introduction
• Example
• Introduction
• Example
• Introduction
• Example
ARRAY

Introduction :

MFC Provides an assortment of array classes for
users
Generic CArray
Class
Nontemplatized
array classes
• Header file Afxtempl.h
• A template class from which
you can create type-safe
arrays for data of any type
• Header file Afxcoll.h
• Is designed to hold a
particular type of data
ARRAY

Type-Specific MFC Array Classes
ARRAY

Sample : CStringArray
Construction
•CStringArray Constructs an empty array for CString objects.
•Example :
•
•
Bounds
•GetSize Gets number of elements in this array.
•GetUpperBound Returns the largest valid index.
•SetSize Sets the number of elements to be contained in this array.
•Example :
•
•
Operators
•operator [] Sets or gets the element at the specified index.
ARRAY

Sample : CStringArray
Operations
•FreeExtra Frees all unused memory above the current upper bound.
•RemoveAll Removes all the elements from this array.
•Example :
•
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.
•GetData Allows access to elements in the array. Can be NULL.
•Example :
•
•
•
ARRAY

CStringArray Class Members
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.
• Append Appends another array to the array; grows the array if necessary.
• Copy Copies another array to the array; grows the array if necessary.
• Example :
•
•
•
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.
• Example :
•
•
•
ARRAY

Generic CArray Class
A template class used to build type-safe array classes
for arbitrary data types, such as array of CPoint
 Users can use data of any kind—even classes of your
own creation—in CArray's template parameters

CArray <DataType, DataType&> variable

Example :
Create an array of CPoint : CArray <CPoint, CPoints&>
array;
 Create an array of SinhVien :
CArray <SinhVien, SinhVien&> array;
array.SetSize(10);
Array.Add(SinhVien(“Tran Van Huy”,”0411255”,”Nam”);

ARRAY

Example :
Request
• Use Array to store and view a list of SinhVien
Step 1
• New a project
• Create SinhVien Class in your project
Step 2
• Add a list of SinhVien into array
• Make the operation of viewing SinhVien Data
EXAMPLE : STEP 1
SinhVien.h
SinhVien.cp
p
Note: must make class Dialog to
be a friend of class SinhVien to
access SinhVien data (Hoten and
EXAMPLE : STEP 2
CollectionDemoDlg::OnInitDialog()
CollectionDemoDlg.
h
Collection Demo
Dialog
Class
Winzards
EXAMPLE : OUTPUT
LIST



A linked list is a collection of items that contain pointers to
other items ( Next and Prev pointer).
The MFC template class CList implements a generic linked
list that can be customized to work with any data type
POSITION : A value used to denote the position of an
element in a collection; used by MFC collection classes
LIST
Sample : CObList

Construction
CObList Constructs an empty list for CObject pointers.
Head/Tail Access
 GetHead Returns the head element of the list (cannot be
empty).
 GetTail Returns the tail element of the list (cannot be
empty).


Operations





RemoveHead Removes the element from the head of the
list.
RemoveTail Removes the element from the tail of the list.
AddHead Adds an element (or all the elements in another
list) to the head of the list (makes a new head).
AddTail Adds an element (or all the elements in another
list) to the tail of the list (makes a new tail).
RemoveAll Removes all the elements from this list.
LIST
Sample : CObList

Iteration
GetHeadPosition Returns the position of the head element
of the list.
 GetTailPosition Returns the position of the tail element of
the list.
 GetNext Gets the next element for iterating.
 GetPrev Gets the previous element for iterating.


Retrieval/Modification
GetAt Gets the element at a given position.
 SetAt Sets the element at a given position.
 RemoveAt Removes an element from this list, specified by
position.

LIST
Sample : CObList

Insertion
InsertBefore Inserts a new element before a given position.
 InsertAfter Inserts a new element after a given position.


Searching
Find Gets the position of an element specified by pointer
value.
 FindIndex Gets the position of an element specified by a
zero-based index.


Status
GetCount Returns the number of elements in this list.
 IsEmpty Tests for the empty list condition (no elements).

LIST

Generic CList Class
A template class used to build type-safe list classes
for arbitrary data types, such as array of CPoint
 Users can use data of any kind—even classes of your
own creation—in CList's template parameters

CList <DataType, DataType&> variable

Example :
Create a list of CPoint : CList <CPoint, CPoints&> list;
 Create an list of Book :
CList <Book, Book&> list;
list

LIST

Example :
Request
•Use List to store and view a list of Book
Step 1
•New a project
•Create Book Class in your project
Step 2
•Add a list of Book into list
•Make the operation of viewing Book Data
EXAMPLE :
STEP 1
Book.h
Book.cpp
EXAMPLE : STEP 2
CCObListDemoDlg.h
Class
Winzards
CCObListDemoDlg::OnInitDial
og()
EXAMPLE : OUTPUT
MAP
A map, also known as a dictionary, is a table of
items keyed by other items
 Maps are ideal containers for large amounts of
data when lookup performance is of paramount
importance

MAP
MFC provides the following type-specific (and
non-template-based) map classes
 Each class includes member functions for adding
and removing items, retrieving items by key, and
enumerating all the items in the map

MAP

Sample : CMapStringToString
Initialize Mapping
• CMapStringToString map;
• map[_T ("Sunday")]
• map[_T ("Monday")]
• map[_T ("Tuesday")]
• map[_T ("Wednesday")]
• map[_T ("Thursday")]
• map[_T ("Friday")]
• map[_T ("Saturday")]
= _T ("Dimanche");
= _T ("Lundi");
= _T ("Mardi");
= _T ("Mercredi");
= _T ("Jeudi");
= _T ("Vendredi");
= _T ("Samedi");
MAP

Sample : CMapStringToString (More in MSDN)
Look up
• CString string;
• if (map.Lookup (_T ("Thursday"), string))
• TRACE (_T ("Thursday in English = %s in French\n"),
string);
• POSITION pos = map.GetStartPosition ();
• while (pos != NULL) {
• CString strKey, strItem;
• map.GetNextAssoc (pos, strKey, strItem);
• TRACE (_T ("Key=%s, Item=%s\n"), strKey, strItem);
•}
END OF WEEK 4