Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
C++ Classes and Data Structures Jeffrey S. Childs
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ STL CSCI 3110.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Programming with ANSI C ++
Standard Template Library
Standard Template Library (STL)
Design Patterns Difficult to describe abstractly Elements:
Standard Template Library
Standard Template Library
An Introduction to STL.
Standard Template Library
Presentation transcript:

Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein

Agenda STL  General Containers  Iterators  Object Functions  STL Examples copyrights © Elhanan Borenstein

General Algorithms, Containers and Iterators copyrights © Elhanan Borenstein

Template Functions / Classes Reminder copyrights © Elhanan Borenstein template void Swap(T& a, T& b) { T temp = a; a = b; b = temp; } void main() { int i=4, j=9; Swap(i, j); char* str1 = “hello”; char* str2 = “world”; Swap(str1, str2); Array iArray(10); Array fArray(10); iArray.Print(); fArray.Print(); } template class Array { T* arr; int size; public: Array(int s) : size(s), arr(new T[size]) {} T& operator[](int index) {return arr[index];} };

A General Find Function Let’s recall the general Find function we saw in the last lecture:  Implement a generic Find() function, which gets: a pointer to the beginning of an array a pointer to the end of an array the value to search for  The function returns the address of the first items in the array that is equal to the given value or the address of one location after the last item if the value was not found. copyrights © Elhanan Borenstein template T* Find(T* begin, T* end, T value) { while (begin != end && *begin != value) begin++; return begin; } void main() { int array[ ] = {3,2,5,7,2,8,11}; int size=sizeof(array)/sizeof(array[0]); int* found; found = Find (&array[0], &array[size], 7); if (found != &array[size]) cout<<“FOUND!!!”<<endl; }  Can Find() work on a linked-list?

A General Find Function Now let’s enhance this function: What restrictions does Find pose on the parameters? Why do we use two types in the template? Can Find() work on other data structure other than arrays? copyrights © Elhanan Borenstein template iterator Find(iterator begin, iterator end, const T& value) { while (begin != end && *begin != value) begin++; return begin; } void main() { int array[ ] = {3,2,5,7,2,8,11}; int size=sizeof(array)/sizeof(array[0]); int* found; found = Find (&array[0], &array[size],7); if (found != &array[size]) cout<<“FOUND!!!”<<endl; }

Iterators It appears that Find() can still work only on arrays, as we are using begin++ to iterate the items. BUT… suppose our data-structure implanted an additional iterator class. This class will support the followings: Introduction & Concepts copyrights © Elhanan Borenstein  Represent an object that can “point” to an item in our data structure.  Overload the operator *, so that using *iter will return the item.  Overload the operator ++ by moving to point on the next item.  Overload the operator ==/!= to compare whether two Iterators points to the same item.  Overload the casting operator to allow casting to item*. The iterator class, “behaves” just like a pointer to data item (item*), although it is actually a completely separate class.

Link List Example copyrights © Elhanan Borenstein data Class item Class T data / / / / Class list head tail Class list ::iterator Example: link_list

General Containers and Iterators A list class that can store a link list of any data type  list  …and many more… A general Find() function that can find an item in any data structure (providing an iterator implementation) holding any data type  Array, Array, …  list, list, list  Tree, Tree, Tree  … and many more… So, what do we have so far? copyrights © Elhanan Borenstein

Object Functions Let’s try to use the same mechanism to send the entire data structure to a template function – Apply() which will perform a certain operation/transformation on all the items of the DS Again, we wish that:  Apply() will work with any data structure (implementing iterators).  The data structure can store any data type.  Apply() will support any operation we wish !!! What’s Next? copyrights © Elhanan Borenstein

Object Functions copyrights © Elhanan Borenstein

Object Functions Our Apply() function will get:  begin iterator  end iterator  operation. In C, when we wanted to send an “operation” as a parameter, we used a function pointer. In C++, we will naturally, use an Object. Introduction copyrights © Elhanan Borenstein

Object Functions copyrights © Elhanan Borenstein struct Sqr { template void operator() (T& number) const { t = t*t; } }; struct Print { template void operator() (const T& printalbe) const { cout<<printable<<endl; } }; #include “sqr.h” #include “print.h” void main() { Sqr MySqrMachine; Print MyPrintMachine; int i = 2; float f = 3.25; Employee em(“John”, 3000); MySqrMachine(i); MySqrMachine(f); MyPrintMachine(i); MyPrintMachine(em); } sqr.h print.h main.cpp

Object Functions copyrights © Elhanan Borenstein struct Reverse { template void operator() (T& reversable) const { reverse(reversable.begin(), reversable.end()); } }; Struct is used, as the entire class is public. Note the format of the operator “()” What restrictions are posed on the parameters send to these object functions? Guidelines void main() { string str = “dlrow olleH”; Reverse rev; rev(str); }

Object Functions copyrights © Elhanan Borenstein Putting is all together… example: apply and now…

STL copyrights © Elhanan Borenstein

STL copyrights © Elhanan Borenstein The classes, and algorithms we saw in the previous slides are actually a simulation of the way STL classes, containers and algorithms work. … and now for a brief overview of what STL really includes. In general STL includes the following components:  Data Structures – (vector, list, set, …)  Generic Algorithms – (for each, find, sort, …)  Object Functions  Allocators Introduction

STL copyrights © Elhanan Borenstein STL provides the following data structures:  vector – dynamic array (supporting resizing)  deque – a queue or a stack  list – doubly linked list  map – Hashtable (key, value pairs)  multimap – Hashtable, supports numerous values stored with each key  set – Hashtable, storing keys only. Each key can only be stored once  multiset – Hashtable, storing keys only. Each key can be stored several times All DS supports: begin(), end(), insert(…), erase(…), clear(...) Data Structures

STL copyrights © Elhanan Borenstein All algorithms appear in the header file Include: for_each (analog to our Apply()), find, find_if, count, replace, copy, sort, reverse and many more. Generic Algorithms Commonly used with STL. Include: pow, sqrt, sin, other math functions, complex numbers functions, logic functions, comparison functions and many more. Object Functions

STL - Examples copyrights © Elhanan Borenstein Example : STL1.cpp Example : STL2.cpp (merging a vector and a list) Names: {She Sells Sea Shealls by the Sea Shore } Number of names starts with S = 6 Numbers Vector { } Numbers List { } Numbers Deque (merged) { }

Questions? copyrights © Elhanan Borenstein