Duke CPS 108 7. 1 Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
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)
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structures Using C++ 2E
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Programming Language Support for Generic Libraries Jeremy Siek and Walid Taha Abstract The generic programming methodology is revolutionizing the way we.
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.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Generic Programming in C++. Generic Parameters Function for squaring a number: Function for squaring a number: sqrt(x) { return x * x; } C version: C.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Templates & STL Stefan Roiser, Lorenzo Moneta CERN PH/SFT.
Overview of C++ Templates
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Moshe Fresko Bar-Ilan University Object Oriented Programming
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.
Cramming for CS 247. FAQ Q: Will you post these slides online? A: Yes.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CINT & Reflex – The Future CINT’s Future Layout Reflex API Work In Progress: Use Reflex to store dictionary data Smaller memory footprint First step to.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
CPS Searching, Maps, Tables l Searching is a fundamentally important operation ä We want to do these operations quickly ä Consider searching using.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
Generic Programming in C++ Giuseppe Attardi Università di Pisa.
Scuola Superiore Sant’Anna Advanced Course on C++ IV Giuseppe Lipari.
Examples (D. Schmidt et al)
TK1924 Program Design & Problem Solving Session 2011/2012
C++ STL Vector Container
Templates. Templates Example: Swap Template Mechanism.
Recitation Outline C++ STL associative containers Examples
CSE 303 Lecture 25 Loose Ends and Random Topics
Lecture 8-2 : STL Iterators and Algorithms
Final Exam Review Inheritance Template Functions and Classes
A dictionary lookup mechanism
Presentation transcript:

Duke CPS Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators are part of an inheritance hierarchy: Iterator ä STL begin, end, *, ++ for pointer like syntax no inheritance, all typedefs in each STL class l What are iterator properties, who makes the iterator ä const, non-const, random-access, … ä makeIterator uses new internally, who deletes? Pointer Proxy: “smart pointers”, allocated on stack but act like heap-allocated pointers

Duke CPS Access to map classes l Both STL and libtapestry use a templated Pair class ä first, second in pair correspond to key, value in map  returned by *iterator and iterator->current() l libtapestry ä includes, insert, getValue: see map.h ä leads to redundant lookups in map implementation l STL ä operator[], insert  map[key] = value; inserts pair if not there  potential problem with if(map[key] == …) l hash_map is NOT standard STL, map uses red-black tree

Duke CPS templates and generic classes/functions l STL is the standard template library ä part of C++ as a library of generic functions, classes, and algorithms ä uses no inheritance, but templates can require existence of operators/functions in hash_map class, operator == is needed in map class, operator < is needed l A templated class is not code, but a code generator ä separation of interface and implementation is tricky ä implementation is a generator, not code ä must instantiate generator at compile time

Duke CPS Using templated functions (classes) l Generic sort template void sort(Vector & a, int numElts) // pre: numElts = number of elements in a // post: a[0] <= a[1] <= … <= a[numElts-1] l What must be true of Type? ä Comparable ä ??? l Instantiate templated function/class ä bind template parameters, unify types Vector vs; … sort(vs,vs.size()); Vector > vvi; … sort(vvi,vvi.size());

Duke CPS templated classes l If interface (.h) and implementation (.cc) are separate, client code must have access to implementation ä can define all code inline, in class (vector.h) ä can explicitly instantiate all uses in a separate file ä can #include “tempclass.cc” in.h file, problems? -frepo flag fixes this in g++ VC++ (and other PC compilers) use this approach l STL classes use inline approach ä see the or header files l Dangers of code-bloat  no literal code sharing between Vector and Vector

Duke CPS Where are bottlenecks? l You can time sections of code ä class Ctimer accessible via “ctimer.h” ä you can use /bin/time or shell time real, system, user l You can profile code ä use the -pg option with compiler and run gprof (see help page) ä instruments code, finds how many times each function is called, what percentage of overall time each uses l You can optimize code ä -O2, -O4 with g++, this can affect debugging ä make it run, make it right, make it fast