Download presentation
Presentation is loading. Please wait.
Published bySamantha Bates Modified over 9 years ago
1
Chapter 5 The MFC Collection Classes
2
How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30 Node * p value: 40 Node * p NULL 10203040 A[4]
3
Make your own Linked List A structure can have a pointer as a member variable struct IHavePointer { int a; int * p; }; struct IHavePointer { int a; int * p; };
4
Make your own Linked List And, more importantly a structure can have a pointer of its type. What can you do with this pointer? struct test { int a; test * p; }; struct test { int a; test * p; };
5
Examples of using a pointer struct test { int a; test * p; }; test ts; ts.a=20; ts.p=&ts; cout<<“ts.a:”<<ts.a<<“\n”; cout a:” a<<“\n”; cout p->a:” p->a<<“\n”; struct test { int a; test * p; }; test ts; ts.a=20; ts.p=&ts; cout<<“ts.a:”<<ts.a<<“\n”; cout a:” a<<“\n”; cout p->a:” p->a<<“\n”;
6
More examples struct Dizzy { int id; Dizzy * p; }; Dizzy a,b,c; a.id = 1;a.p=&b; b.id = 2; b.p=&c; c.id = 3; c.p=&a; cout<<“a.id:”<<a.id<<“\n”; cout id<<“\n”; cout p->id<<“\n”; struct Dizzy { int id; Dizzy * p; }; Dizzy a,b,c; a.id = 1;a.p=&b; b.id = 2; b.p=&c; c.id = 3; c.p=&a; cout<<“a.id:”<<a.id<<“\n”; cout id<<“\n”; cout p->id<<“\n”;
7
Linking data (linked list)
8
Create 4 variables of Node data type. Name the variables as ‘a’, ‘b’, ‘c’ and ‘d’. Link the variables as shown below. Print all the values starting from ‘a’ by using ‘while’ statement. Coding practice value: 10 Node * p struct Node { int value; Node * p; }; struct Node { int value; Node * p; }; a a value: 20 Node * p b b value: 30 Node * p c c value: 40 Node * p d d NULL
9
Template and STL
10
What is the Template? tem·plate n. 1 본뜨는 공구 ( 工具 ), 형판 ( 型板 ) 2 【건축】 보받이, 도리받이 3 조선대 ( 造船臺 ) 의 쐐기 ;( 반 ) 투명의 피복지 ( 彼覆紙 ) 4 【생화학】 ( 핵산의 ) 주형 ( 鑄型 ) 5 【컴퓨터】 보기판, 템플릿 《키보드 위에 놓고 각 키에 할당된 명령의 내용을 보이는 시트》
11
Generic programming A ‘max’ function to return a bigger value between two input integer numbers int max(int a, int b) { if(a>b) return a; else return b; } int max(int a, int b) { if(a>b) return a; else return b; } How about two float numbers? We need OVERLOADING!
12
Generic programming A ‘max’ function to return a bigger value between two input f,loat numbers float max(float a, float b) { if(a>b) return a; else return b; } float max(float a, float b) { if(a>b) return a; else return b; } How about two double numbers??? We need OVERLOADING! How about two char variables??? We need OVERLOADING! How about two complex numbers?? We need OVERLOADING!
13
How can we solve it at once: Generic programming Template!!!
14
Go back to Template
15
How to use the Template in C++ Template class Template function
16
A Smart Array class AutoArray { public: AutoArray(int * ptr) { _ptr = ptr; } ~AutoArray() {delete [] _ptr; } int& operator[] (int index) {return _ptr[index];} private: int * _ptr; }; class AutoArray { public: AutoArray(int * ptr) { _ptr = ptr; } ~AutoArray() {delete [] _ptr; } int& operator[] (int index) {return _ptr[index];} private: int * _ptr; };
17
A Smart Array int main() { AutoArray arr( new int[100] ); arr[20] = 30; return 0; } int main() { AutoArray arr( new int[100] ); arr[20] = 30; return 0; } AutoArray only works with integers
18
Why only integers? class AutoArray { public: AutoArray(int * ptr) { _ptr = ptr; } ~AutoArray() {delete [] _ptr; } int& operator[] (int index) {return _ptr[index];} private: int * _ptr; }; class AutoArray { public: AutoArray(int * ptr) { _ptr = ptr; } ~AutoArray() {delete [] _ptr; } int& operator[] (int index) {return _ptr[index];} private: int * _ptr; };
19
Use Template for Class Smart array for all kinds of data type template class AutoArray { public: AutoArray(T* ptr) {_ptr = ptr;} ~AutoArray() {delete[] _ptr;} T& operator[] (int index) {return _ptr[index];} private: T* _ptr; };
20
Use Template for Class Smart array for all kinds of data type int main() { AutoArray arr( new float [100] ); arr[0] = 99.99f; return 0; }
21
The usage of the template Use ‘A’ instead of class name Class declaration Using template Create a variable using template class
22
What happen when using template When creating a variable the proper class is created by the complier –Your code looks like this template class TwoArray { // skip the details A arr1[ MAX ]; B arr2[ MAX ]; }; TwoArray arr;
23
What happen when using template When creating a variable the proper class is created by the complier –Compiler creates a new code like this class TwoArray_char_double_20 // Name will be different { // skip the details char arr1[ 20 ]; double arr2[ 20 ]; }; TwoArray_char_double_20 arr;
24
Standard Template Library C++ = A Programming Language –Class, Inheritance, template, overloading… STL = A Toolset by using C++ –A package consisting of classes and functions –Providing many combinient data structures instead of array or pointer
25
Standard Template Library What are inside: –Container A data structure classes list, vector, deque, … –Iterator Access to the items in the container ( = pointer) –Algorithm shuffle, replace, fill, remove, sort, …
26
MFC Container Class CArray CList CVector CMap CArray CList CVector CMap
27
Linked List Good: Easy to add or delete data Bad: Only sequential access to the data is possible
28
CList Linked List Template Class by MFC Create a variable: CList a; Add Data: CList::AddTail(..) CList::AddHead(..) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) Create a variable: CList a; Add Data: CList::AddTail(..) CList::AddHead(..) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) Example: CList a; Add data: CList::AddTail(3) CList::AddHead(4) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) Example: CList a; Add data: CList::AddTail(3) CList::AddHead(4) Delete Data: CList::RemoveTail(); CList::RemoveHeat(); CList::RemoveAt(..) iterator: POSITION iterator: POSITION
29
Iterator Access to an item in the Container (== pointer) iterator datatype in MFC : POSITION CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); // Return the first data while(pos != NULL) // position { int value = a.GetNext(pos); // Return data at pos // pos indicate the next } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); // Return the first data while(pos != NULL) // position { int value = a.GetNext(pos); // Return data at pos // pos indicate the next }
30
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value
31
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value
32
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value 10 1 st call
33
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value 20 2 nd call
34
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value 30 3 rd call
35
Iterator CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } CList a; a.AddTail(10); a.AddTail(20); a.AddTail(30); POSITION pos = a.GetHeadPosition(); while(pos != NULL) { int value = a.GetNext(pos); } 10 20 30 NULL a pos value 30 end
36
Insertion // Add at front CList::AddHead( value ) // Add at back CList::AddTail( value ) // Add at the given position CList::InsertAfter (POSITION, value) // Add at front CList::AddHead( value ) // Add at back CList::AddTail( value ) // Add at the given position CList::InsertAfter (POSITION, value)
37
Retrieval/Modification // Get value value = CList::GetAt( POSITION ) // Get reference value & = CList::GetAt( POSITION ) // Get value value = CList::GetAt( POSITION ) // Get reference value & = CList::GetAt( POSITION ) CList a; a.AddHead(10); a.AddHead(20); a.AddHead(30); CList a; a.AddHead(10); a.AddHead(20); a.AddHead(30); POSITION pos; pos = a.GetHeadPosition(); int b = a.GetAt(pos);// value int &c = a.GetAt(pos); // reference POSITION pos; pos = a.GetHeadPosition(); int b = a.GetAt(pos);// value int &c = a.GetAt(pos); // reference
38
Removal // Delete the frist CList::RemoveHead( ) // Delete the end CList::RemoveTail( ) // Delete at the position CList::RemoveAt(POSITION) // Delete all CList::RemoveAll( ) // Delete the frist CList::RemoveHead( ) // Delete the end CList::RemoveTail( ) // Delete at the position CList::RemoveAt(POSITION) // Delete all CList::RemoveAll( )
39
Coding Practice Draw points with mouse left dragging. Delete points with mouse right dragging Draw Points with mouse left dragging Set a rectangle By mouse right dragging Delete points inside the rectangle
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.