Chapter 5 The MFC Collection Classes. 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.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

Chapter 17 Linked Lists.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
LIST PROCESSING.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Data Structures Using C++ 2E
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Containers Overview and Class Vector
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Pointers OVERVIEW.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Working with Pointers An exercise in destroying your computer.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Exceptions & Templates
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear.
UNIT-II Topics to be covered Singly linked list Circular linked list
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Exceptions, Templates, and the Standard Template Library (STL)
Linked lists.
Visit for more Learning Resources
Sequences 9/18/ :20 PM C201: Linked List.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Buy book Online -
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Chapter 5 The MFC Collection Classes
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Lesson 25 Miscellaneous Topics
Linked lists.
Templates CMSC 202, Version 4/02.
Presentation transcript:

Chapter 5 The MFC Collection Classes

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 A[4]

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; };

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; };

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

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

Linking data (linked list)

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

Template and STL

What is the Template? tem·plate n. 1 본뜨는 공구 ( 工具 ), 형판 ( 型板 ) 2 【건축】 보받이, 도리받이 3 조선대 ( 造船臺 ) 의 쐐기 ;( 반 ) 투명의 피복지 ( 彼覆紙 ) 4 【생화학】 ( 핵산의 ) 주형 ( 鑄型 ) 5 【컴퓨터】 보기판, 템플릿 《키보드 위에 놓고 각 키에 할당된 명령의 내용을 보이는 시트》

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!

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!

How can we solve it at once: Generic programming Template!!!

Go back to Template

How to use the Template in C++ Template class Template function

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; };

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

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; };

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; };

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; }

The usage of the template Use ‘A’ instead of class name Class declaration Using template Create a variable using template class

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;

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;

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

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, …

MFC Container Class CArray CList CVector CMap CArray CList CVector CMap

Linked List Good: Easy to add or delete data Bad: Only sequential access to the data is possible

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

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 }

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); } NULL a pos value

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); } NULL a pos value

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); } NULL a pos value 10 1 st call

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); } NULL a pos value 20 2 nd call

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); } NULL a pos value 30 3 rd call

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); } NULL a pos value 30 end

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)

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

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

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