Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A

2 C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction Example

3 A RRAY Introduction : MFC Provides an assortment of array classes for users Header file Afxtempl.h A template class from which you can create type-safe arrays for data of any type Generic CArray Class Header file Afxcoll.h Is designed to hold a particular type of data Nontemplatized array classes

4 A RRAY Type-Specific MFC Array Classes

5 A RRAY Sample : CStringArray Construction CStringArray Constructs an empty array for CString objects.CStringArray Example : Bounds GetSize Gets number of elements in this array.GetSize GetUpperBound Returns the largest valid index.GetUpperBound SetSize Sets the number of elements to be contained in this array.SetSize Example : Operators operator [] Sets or gets the element at the specified index.operator []

6 A RRAY Sample : CStringArray Operations FreeExtra Frees all unused memory above the current upper bound.FreeExtra RemoveAll Removes all the elements from this array.RemoveAll Example : Element Access GetAt Returns the value at a given index.GetAt SetAt Sets the value for a given index; array not allowed to grow.SetAt ElementAt Returns a temporary reference to the element pointer within the array.ElementAt GetData Allows access to elements in the array. Can be NULL.GetData Example :

7 A RRAY CStringArray Class Members Growing the Array SetAtGrow Sets the value for a given index; grows the array if necessary.SetAtGrow Add Adds an element to the end of the array; grows the array if necessary.Add Append Appends another array to the array; grows the array if necessary.Append Copy Copies another array to the array; grows the array if necessary.Copy Example : Insertion/Removal InsertAt Inserts an element (or all the elements in another array) at a specified index.InsertAt RemoveAt Removes an element at a specific index.RemoveAt Example :

8 A RRAY 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 array; Create an array of SinhVien : CArray array; array.SetSize(10); Array.Add(SinhVien(“Tran Van Huy”,”0411255”,”Nam”); CArray variable

9 A RRAY 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

10 E XAMPLE : S TEP 1 SinhVien.h SinhVien.cp p Note: must make class Dialog to be a friend of class SinhVien to access SinhVien data (Hoten and MSSV)

11 E XAMPLE : S TEP 2 Class Winzards Collection Demo Dialog CollectionDemoDlg. h CollectionDemoDlg::OnInitDialog()

12 E XAMPLE : O UTPUT

13 L IST 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

14 L IST Sample : CObList Construction CObList Constructs an empty list for CObject pointers. Head/Tail Access CObList GetHead Returns the head element of the list (cannot be empty). GetHead GetTail Returns the tail element of the list (cannot be empty). GetTail Operations RemoveHead Removes the element from the head of the list. RemoveHead RemoveTail Removes the element from the tail of the list. RemoveTail AddHead Adds an element (or all the elements in another list) to the head of the list (makes a new head). AddHead AddTail Adds an element (or all the elements in another list) to the tail of the list (makes a new tail). AddTail RemoveAll Removes all the elements from this list. RemoveAll

15 L IST Sample : CObList Iteration GetHeadPosition Returns the position of the head element of the list. GetHeadPosition GetTailPosition Returns the position of the tail element of the list. GetTailPosition GetNext Gets the next element for iterating. GetNext GetPrev Gets the previous element for iterating. GetPrev Retrieval/Modification GetAt Gets the element at a given position. GetAt SetAt Sets the element at a given position. SetAt RemoveAt Removes an element from this list, specified by position. RemoveAt

16 L IST Sample : CObList Insertion InsertBefore Inserts a new element before a given position. InsertBefore InsertAfter Inserts a new element after a given position. InsertAfter Searching Find Gets the position of an element specified by pointer value. Find FindIndex Gets the position of an element specified by a zero-based index. FindIndex Status GetCount Returns the number of elements in this list. GetCount IsEmpty Tests for the empty list condition (no elements). IsEmpty

17 L IST 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 list; Create an list of Book : CList list; list CList variable

18 L IST 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

19 E XAMPLE : STEP 1 Book.h Book.cpp

20 E XAMPLE : S TEP 2 Class Winzards CCObListDemoDlg.h CCObListDemoDlg::OnInitDial og()

21 E XAMPLE : O UTPUT

22 M AP 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

23 M AP 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

24 M AP Sample : CMapStringToString Initialize Mapping CMapStringToString map; map[_T ("Sunday")] = _T ("Dimanche"); map[_T ("Monday")] = _T ("Lundi"); map[_T ("Tuesday")] = _T ("Mardi"); map[_T ("Wednesday")] = _T ("Mercredi"); map[_T ("Thursday")] = _T ("Jeudi"); map[_T ("Friday")] = _T ("Vendredi"); map[_T ("Saturday")] = _T ("Samedi");

25 M AP 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); }

26 E ND OF W EEK 4


Download ppt "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."

Similar presentations


Ads by Google