Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
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.
Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
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.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
STL !!!generic programming!!! Anar Manafov
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Generic Programming Using the C++ Standard Template Library.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
 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.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Overview of C++ Templates
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Lecture 7 : Intro. to STL (Standard Template Library)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
1 The Standard Template Library Drozdek Section 3.7.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Mark Redekopp David Kempe
Standard Template Library
Introduction to olympic programming
C++ Templates.
Lecture 7-2 : STL Iterators
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Generic Programming Techniques in C++
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
priority_queue<T>
Recursive Linked List Operations
Iterators and STL Containers
Lecture 8-2 : STL Iterators and Algorithms
STL (Standard Template Library)
Lesson 25 Miscellaneous Topics
C++ Programming: chapter 10 – STL
An Introduction to STL.
Presentation transcript:

Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Polymorphism and Templates Our Shape pointers can point to any of it's subclass. Shape *s; Circle *c = new Circle(6); s = c; It is impossible to tell at compile time what Shape will be pointing to at compile time so how can we find out what Shape is really pointing to? Through a generic templated function!

dynamic_cast<>() This function allows us to determine the type of a pointer at run-time. dynamic_cast<>() fn_nametemplate_parameterpointer_instance Shape *s = c; Circle *c1 = dynamic_cast (s);

Generic Classes and C++ Templates A generic class can be thought of as an abstract specification of a set of classes It is a recipe for producing classes An instance of a generic class is a class (An instance of a class is an object) In C++, generic classes are defined by templates Templates can be used to produce functions and classes C++’s template facility is very powerful (surprise surprise!) but it can become very complicated when combined with other language features

Function Templates … a swap... template void swap (T& x, T& y){ T temp = x; x = y; y = temp;} int main (){ int a = 1, b =2; cout << "a is "<< a<<", b is "<<b<<endl; swap (a,b); cout << "a is "<< a<<", b is "<<b<<endl; } a is 1, b is 2 a is 2, b is 1 This outputs: This example can now be extended to show the power of the generic swap function

Function Templates … a swap... template void swap (T& x, T& y){ T temp = x; x = y; y = temp;} int main (){ int a = 1, b =2; cout << "a is "<< a<<", b is "<<b<<endl; swap (a,b); cout << "a is "<< a<<", b is "<<b<<endl; char c = 'c', d = 'd'; cout << "c is "<< c<<", d is "<<d<<endl; swap (c,d); cout << "c is "<< c<<", d is "<<d<<endl; } a is 1, b is 2 a is 2, b is 1 c is c, d is d c is d, d is c This outputs:

Question: Why should I write Stack, List, Set, Sort, Search, etc? Answer: don’t!!! use the C++ Standard Library. Heart of the library: STL

The standard C++ library has: Containers run through Iterators to “run through” the containers Algorithms that work on containers: find, sort, etc. container adapters: stack, queue, priority_queue iterator adapters strings

containers grow as needed -- good to manage data, as used in typical programs: programs start with an empty container, read or compute data, store it, access it, store some more… built in arrays don’t grow push_back all 3 sequential containers have push_back

Some functions for sequential containers: push_back(), pop_back() push_front(), pop_front() size() begin() -- points to first item in vector end() -- points one past end of vector iterator: pointer to an item in the vector

C++ strings are containers: better than C strings Memory managed for you: C++ ==> string s(“dog”) C ==> s = new char[4]; strcpy(s, “dog”) easier to use C++ ==> s <= t C ==> strcmp(s, t) <= 0 less error prone than C strings

Iterators step through a container, independent of internal structure small but common interface for any container every container has its own iterator interface for iterators almost the same as pointers: *, ++, --

Iterator Use list MyList; for( list ::iterator ptr = MyList.begin() ; ptr ! = MyList.end(); ptr++; ) { std::cout<<*ptr; }

#include const int MAX = 1024; void print(const vector & n) { for (int i = 0; i < n.size(); ++i) cout << n[i] << endl; } int main() { vector items; cout << “size of items is: “ << items.size() << endl; items.push_back(rand() % 100); cout << “size of items is: “ << items.size() << endl; for (int i = 0; i < MAX; ++i) items.push_back(rand() %100); cout << “size of items is: “ << items.size() << endl; print(items); } 14

Algorithms container algorithm iterator container Algorithms process the elements of a container Algorithms use iterators Separation of data and operations: an algorithm can be written once and work with arbitrary containers because iterators have a common interface.

Namespaces Used by Standard C++ Library 16

Name clashes current software trends: build libraries, modules, components when combined, names can clash Namespaces solve this problem example: namespace NS { class Student {}; void print(); int x; }

3 ways to access namespace items with the scope qualifier: NS::x; qualifier with using: using NS::x; the using directive: using namespace NS;

C++ Standard library -- std All identifiers are in namespace std Some compilers enforce use of std namespace gcc’s C++ compiler does not enforce std until std::cout << std::hex << 3.4 << std::endl;