MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array...

26
MFC COLLECTION CLASSES Trần Anh Tuấn A

Transcript of MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array...

Page 1: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

MFC COLLECTION CLASSESTrần Anh Tuấn A

Page 2: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

CONTENTS ( MFC COLLECTION CLASSES )

Page 3: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

Introduction : MFC Provides an assortment of array classes for

users

Page 4: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

Type-Specific MFC Array Classes

Page 5: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

Sample : CStringArray

Page 6: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

Sample : CStringArray

Page 7: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

CStringArray Class Members

Page 8: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

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”);

CArray <DataType, DataType&> variable

Page 9: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

ARRAY

Example :

Page 10: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE : STEP 1SinhVien.h

SinhVien.cpp

Note: must make class Dialog to be a friend of class SinhVien to access SinhVien data (Hoten and MSSV)

Page 11: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE : STEP 2

Class Winzards

Collection Demo Dialog

CollectionDemoDlg.h

CollectionDemoDlg::OnInitDialog()

Page 12: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE : OUTPUT

Page 13: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

Page 14: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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.

Page 15: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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.

Page 16: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

Page 17: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

Example : Create a list of CPoint : CList <CPoint, CPoints&> list; Create an list of Book :

CList <Book, Book&> list; list

CList <DataType, DataType&> variable

Page 18: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

LIST

Example :

Page 19: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE :STEP 1

Book.h

Book.cpp

Page 20: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE : STEP 2

Class Winzards

CCObListDemoDlg.h

CCObListDemoDlg::OnInitDialog()

Page 21: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

EXAMPLE : OUTPUT

Page 22: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

Page 23: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

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

Page 24: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

MAP

Sample : CMapStringToString

Page 25: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

MAP

Sample : CMapStringToString (More in MSDN)

Page 26: MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.

END OF WEEK 4