Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 19-2 Learning Objectives Iterators Constant and mutable.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 13 Recursion. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Recursive void Functions Tracing recursive.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 8 Operator Overloading, Friends, and References.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Data Structures Using C++ 2E
Vectors, lists and queues
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
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.
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.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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.
Chapter 20 Patterns and UML. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Patterns  Adapter pattern  Model-View-Controller.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 19 Standard Template Library Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
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)
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Intro to the C++ STL Timmie Smith September 6, 2001.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CS212: Object Oriented Analysis and Design
Standard Template Library
Programming with ANSI C ++
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Today’s Learning Objective
CS212: Object Oriented Analysis and Design
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Standard Template Library (STL)
Introduction: Some Representative Problems
Presentation transcript:

Chapter 19 Standard Template Library

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable iterators Reverse iterators Containers Sequential containers Container adapters stack and queue Generic Algorithms Big-O notation Sequence, set, and sorting algorithms

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Introduction Recall stack and queue data structures We created our own Large collection of standard data structures exists Make sense to have standard portable implementations of them! Standard Template Library (STL) Includes libraries for all such data structures Like container classes: stacks and queues

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterators Recall: generalization of a pointer Typically even implemented with pointer! "Abstraction" of iterators Designed to hide details of implementation Provide uniform interface across different container classes Each container class has "own" iterator type Similar to how each data type has own pointer type

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Manipulating Iterators Recall using overloaded operators: ++, --, ==, != * So if p is iterator variable, *p gives access to data pointed to by p Vector template class Has all above overloads Also has members begin() and end() c.begin();//Returns iterator for 1 st item in c c.end();//Returns "test" value for end

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Cycling with Iterators Recall cycling ability: for (p=c.begin();p!=c.end();p++) process *p//*p is current data item Big picture so far… Keep in mind: Each container type in STL has own iterator types Even though theyre all used similarly

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.1 Iterators Used with a Vector (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.1 Iterators Used with a Vector (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Iterator Types Iterators for vectors of ints are of type: std::vector ::iterator Iterators for lists of ints are of type: std::list ::iterator Vector is in std namespace, so need: using std::vector ::iterator;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Kinds of Iterators Different containers different iterators Vector iterators Most "general" form All operations work with vector iterators Vector container great for iterator examples

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Access: Display 19.2 Bidirectional and Random-Access Iterator Use

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterator Classifications Forward iterators: ++ works on iterator Bidirectional iterators: Both ++ and – work on iterator Random-access iterators: ++, --, and random access all work with iterator These are "kinds" of iterators, not types!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Constant and Mutable Iterators Dereferencing operators behavior dictates Constant iterator: * produces read-only version of element Can use *p to assign to variable or output, but cannot change element in container E.g., *p = ; is illegal Mutable iterator: *p can be assigned value Changes corresponding element in container i.e.: *p returns an lvalue

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Reverse Iterators To cycle elements in reverse order Requires container with bidirectional iterators Might consider: iterator p; for (p=container.end();p!=container.begin(); p--) cout << *p << " " ; But recall: end() is just "sentinel", begin() not! Might work on some systems, but not most

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Reverse Iterators Correct To correctly cycle elements in reverse order: reverse_iterator p; for (rp=container.rbegin();rp!=container.rend(); rp++) cout << *rp << " " ; rbegin() Returns iterator at last element rend() Returns sentinel "end" marker

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Compiler Problems Some compilers problematic with iterator declarations Consider our usage: using std::vector ::iterator; … iterator p; Alternatively: std::vector ::iterator p; And others… Try various forms if compiler problematic

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Containers Container classes in STL Different kinds of data structures Like lists, queues, stacks Each is template class with parameter for particular data type to be stored e.g., Lists of ints, doubles or myClass types Each has own iterators One might have bidirectional, another might just have forward iterators But all operators and members have same meaning

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Sequential Containers Arranges list data 1 st element, next element, … to last element Linked list is sequential container Earlier linked lists were "singly linked lists" One link per node STL has no "singly linked list" Only "doubly linked list": template class list

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.4 Two Kinds of Lists

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.5 Using the list Template Class(1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.5 Using the list Template Class(2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Container Adapters stack and queue Container adapters are template classes Implemented "on top of" other classes Example: stack template class by default implemented on top of deque template class Buried in stacks implementation is deque where all data resides Others: queue, priority_queue

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Specifying Container Adapters Adapter template classes have "default" containers underneath But can specify different underlying container Examples: stack template class any sequence container priority_queue default is vector, could be others Implementing Example: stack > Makes vector underlying container for stack

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Associative Containers Associative container: simple database Store data Each data item has key Example: data: employees record as struct key: employees SSN Items retrieved based on key

Copyright © 2006 Pearson Addison-Wesley. All rights reserved set Template Class Simplest container possible Stores elements without repetition 1 st insertion places element in set Each element is own key Capabilities: Add elements Delete elements Ask if element is in set

Copyright © 2006 Pearson Addison-Wesley. All rights reserved More set Template Class Designed to be efficient Stores values in sorted order Can specify order: set s; Ordering is well-behaved ordering relation that returns bool None specified: use < relational operator

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Map Template Class A function given as set of ordered pairs For each value first, at most one value second in map Example map declaration: map numberMap; Stores in sorted order, like set Second value can have no ordering impact

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Efficiency STL designed with efficiency as important consideration Strives to be optimally efficient Example: set, map elements stored in sorted order for fast searches Template class member functions: Guaranteed maximum running time Called "Big-O" notation, an "efficiency"-rating

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Generic Algorithms Basic template functions Recall algorithm definition: Set of instructions for performing a task Can be represented in any language Typically thought of in "pseudocode" Considered "abstraction" of code Gives important details, but not find code details STLs algorithms in template functions: Certain details provided only Therefore considered "generic algorithms"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Running Times How fast is program? "Seconds"? Consider: large input?.. small input? Produce "table" Based on input size Table called "function" in math With arguments and return values! Argument is input size: T(10), T(10,000), … Function T is called "running time"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Table for Running Time Function: Display Some Values of a Running Time Function

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Consider Sorting Program Faster on smaller input set? Perhaps Might depend on "state" of set "Mostly" sorted already? Consider worst-case running time T(N) is time taken by "hardest" list List that takes longest to sort

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Counting Operations T(N) given by formula, such as: T(N) = 5N + 5 "On inputs of size N program runs for 5N + 5 time units" Must be "computer-independent" Doesnt matter how "fast" computers are Cant count "time" Instead count "operations"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Counting Operations Example int I = 0; bool found = false; while (( I < N) && !found) if (a[I] == target) found = true; else I++; 5 operations per loop iteration: <, &&, !, [ ], ==, ++ After N iterations, final three: <, &&, ! So: 6N+5 operations when target not found

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Big-O Notation Recall: 6N+5 operations in "worst-case" Expressed in "Big-O" notation Some constant "c" factor where c(6N+5) is actual running time c different on different systems We say code runs in time O(6N+5) But typically only consider "highest term" Term with highest exponent O(N) here

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Big-O Terminology Linear running time: O(N)directly proportional to input size N Quadratic running time: O(N 2 ) Logarithmic running time: O(log N) Typically "log base 2" Very fast algorithms!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Comparison of Running Times

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Container Access Running Times O(1) - constant operation always: Vector inserts to front or back deque inserts list inserts O(N) Insert or delete of arbitrary element in vector or deque (N is number of elements) O(log N) set or map finding

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Nonmodifying Sequence Algorithms Template functions operating on containers NO modification of container contents Generic find function Typical example Can be used with any STL sequence container class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display The Generic find Function (1 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display The Generic find Function (2 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display The Generic find Function (3 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Modifying Sequence Algorithms STL functions that change container contents Recall: adding/removing elements from containers can affect other iterators! list, slist guarantee no iterator changes vector, deque make NO such guarantee Always watch which iterators are assured to be changed/unchanged

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Set Algorithms STL generic set operation functions All assume containers stored in sorted order Containers set, map, multiset, multimap DO store in sorted order, so all set functions apply Others, like vector, are not sorted Should not use set functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Sorting Algorithms STL contains two template functions: 1. sort range of elements 2. merge two sorted ranges of elements Guaranteed running time O(N log N) No sort can be faster Function guarantees fastest possible sort

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 1 Iterator is "generalization" of a pointer Used to move through elements of container Container classes with iterators have: Member functions end() and begin() to assist cycling Main kinds of iterators: Forward, bi-directional, random-access Given constant iterator p, *p is read-only version of element

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 2 Given mutable iterator p *p can be assigned value Bidirectional container has reverse iterators allowing reverse cycling Main STL containers: list, vector, deque stack, queue: container adapter classes set, map, multiset, multimap containers store in sorted order STL implements generic algorithms Provide maximum running time guarantees