Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
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.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Templates & STL 1.Generic Programming 2.C++ Templates 3.Function Templates 4.Class Templates 5.Standard Template Library (STL) 6.Vectors 7.Iterators 1.
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
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
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.
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
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
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.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
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.
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.
Duke CPS Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Motivation for Generic Programming in C++
Standard Template Library
CS212: Object Oriented Analysis and Design
TK1924 Program Design & Problem Solving Session 2011/2012
How to be generic Lecture 10
Template Classes and Functions
Exceptions, Templates, and the Standard Template Library (STL)
Vectors Holds a set of elements, like an array
C++ Standard Library.
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Array Lists Chapter 6 Section 6.1 to 6.3
Vectors.
C++ STL Vector Container
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Constructors and Other Tools
Standard Version of Starting Out with C++, 4th Edition
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Collections Intro What is the STL? Templates, collections, & iterators
COP 3330 Object-oriented Programming in C++
Lecture 8: Introduction to C++ Templates and Exceptions
14 – Sequential Containers
Presentation transcript:

Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base class is instantiated using a concrete type. Templates are powerful because they allow producing generic classes! C++ Templates

Template class example: template class MyStorageTemplate { public: T Get(int index); // template method void Put(int index); // template method private: T Data[N]; // private data field }; Template function example: template void MyTemplateFuncion(T param); Validity of template definition is evaluated at compile time it depends on the concrete type used to instantiate the template. C++ Template Declaration

The Standard Template Library (STL) is a software library included in the C++ Standard Library. It provides containers, iterators, and algorithms. More specifically the C++ Standard Library is based on the STL Library published by SGI. Both include some features not found in the other. SGI's STL is rigidly specified as a set of headers, while ISO C++ does not specify header content, and allows implementation either in the headers, or in a true library. STL was architected by Alexander Stepanov in Standard Template Library

The quality of the C++ compiler has a large impact on usability of STL: Error messages involving templates are difficult to decipher. Excessive usage of STL templates leads to code bloat. Template instantiation tends to increase compilation time and memory usage (sometimes by as much as an order of magnitude). STL implementations non-standardized. STL Disadvantages

Header file: #include Declaration: template <class Type, class Allocator = allocator > class vector; Template instantiation: vector myStringVector; STL vector Class

vectorConstructor. assignErases a vector and copies the specified elements to the empty vector. atReturns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. beginReturns a random-access iterator to the first element in the container. capacityReturns the number of elements that the vector could contain without reallocating. clearErases the elements of the vector. emptyTests if the vector container is empty. endReturns a random-access iterator that points just beyond the end of the vector. eraseRemoves an element or a range of elements in a vector from specified positions. frontReturns a reference to the first element in a vector. insertInserts an element or a number of elements into the vector at a specified position. max_sizeReturns the maximum length of the vector. pop_backDeletes the element at the end of the vector. push_backAdd an element to the end of the vector. rbeginReturns an iterator to the first element in a reversed vector. rendReturns an iterator to the end of a reversed vector. resizeSpecifies a new size for a vector. reserveReserves a minimum length of storage for a vector object. sizeReturns the number of elements in the vector. swapExchanges the elements of two vectors. STL vector Members

vectorConstructor. assignErases a vector and copies the specified elements to the empty vector. atReturns a reference to the element at a specified location in the vector. backReturns a reference to the last element of the vector. beginReturns a random-access iterator to the first element in the container. capacityReturns the number of elements that the vector could contain without reallocating. clearErases the elements of the vector. emptyTests if the vector container is empty. endReturns a random-access iterator that points just beyond the end of the vector. eraseRemoves an element or a range of elements in a vector from specified positions. frontReturns a reference to the first element in a vector. insertInserts an element or a number of elements into the vector at a specified position. max_sizeReturns the maximum length of the vector. pop_backDeletes the element at the end of the vector. push_backAdd an element to the end of the vector. rbeginReturns an iterator to the first element in a reversed vector. rendReturns an iterator to the end of a reversed vector. resizeSpecifies a new size for a vector. reserveReserves a minimum length of storage for a vector object. sizeReturns the number of elements in the vector. swapExchanges the elements of two vectors. vector Members

Write a program that: - Initializes a vector based on user input by reading vector size and elements from cin -removes or reorganizes vector elements base on some criteria vector Exercise

template class Vector { public: Vector(int initialSize); enum Position {Start = 0, End = -1}; int GetSize() const; T& operator[](int index) const; void InsertAt(int index, const T& element); void RemoveAt(int index, const T& element); private: int Size; T* Data; void Reallocate(int newSize); }; Use memcpy for memory copy operations. Make the class verify parameters and throw exceptions. You can use either new or malloc to allocate / reallocate memory. Write Your Own Vector