1 Chapter 1 C++ Templates Sections 1.6 and 1.7. 2 Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Chapter 20 The STL (containers, iterators, and algorithms) John Keyser’s Modifications of Slides by Bjarne Stroustrup
. 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.
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
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)
. 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.
More on the STL vector list stack queue priority_queue.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
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,
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
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.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates and the STL.
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
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.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
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.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Duke CPS Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.
Standard Template Library
C++ Templates.
Exceptions, Templates, and the Standard Template Library (STL)
Vectors Holds a set of elements, like an array
C++ Standard Library.
Starting Out with C++ Early Objects Eighth Edition
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
C++ STL Vector Container
Standard Template Library Model
Standard Template Library
Standard Version of Starting Out with C++, 4th Edition
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Exceptions, Templates, and the Standard Template Library (STL)
C++ STL Stack, Queue, and Deque
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

1 Chapter 1 C++ Templates Sections 1.6 and 1.7

2 Templates Type-independent patterns that can work with multiple data types –Generic programming –Code reusable Function Templates –These define logic behind the algorithms that work for multiple data types Class Templates –These define generic class patterns into which specific data types can be plugged in to produce new classes

3 Function Templates Algorithms may use the same logic for different data types –Different definitions are written just to deal with different data types –Templates permit a common code, independent of the data type –Templates should be in header files, so that the compiler will know how to generate code when it sees the template used Examples –Lec4/largest.h/cpp, uselargest.cpp Error with template in the.cpp file $ g++ uselargest.cpp largest.cpp /tmp/ccAg3qmY.o: In function `main': uselargest.cpp:(.text+0x133): undefined reference to `int tlargest (int*, int)' uselargest.cpp:(.text+0x16c): undefined reference to `float tlargest (float*, int)'

4 Class Templates Identical code can be used for classes that have data of different types Examples –Lec4/pair.h, usepair.cpp –Use friend function to access private members (example, for IO) The approach used in the text is also ok –Note the use of a space between > and > in usepair.cpp This makes the compiler realize that >> is not a single token

5 An Alternative to friend Use a public ‘print’ function and an operator that calls it

6 STL Containers Container: stores other objects –Common operations such as: push_back, back, front, [ ], etc Not all operations are defined for all containers –Sequence: positions of elements are important Example: vector, list –Associative containers: elements are accessed through keys Example: map, set, pair Example –Lec4/vlm.cpp for example with vector, list, and map

7 STL Iterators Iterator: like a pointer to elements of a container –Used to access elements of a container –Popular operators * (dereference), ++, --, ==, != –Examining all the elements of a container c.begin() returns an iterator to the first element of container c c.end returns a pointer to one past the end

8 Matrices C++ library does not provide a matrix class Constructor –Creates rows number of zero-sized vectors –Resizes each vector to col elements Two types of [ ] operators –One for LHS that returns by reference –Another for RHS that returns by constant reference to[i] = from[i] –So we have two very identical functions What makes their signatures different?