Chapter 17 Templates - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 17 Templates

Description:

... type :: iterator name; ordinary iterator needs ... Need to initialize to point to location first then manipulate ... Different definition than dictionary ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 19
Provided by: RalphFTo5
Category:

less

Transcript and Presenter's Notes

Title: Chapter 17 Templates


1
Chapter 17 Templates
2
Function Templates
  • Express general form for a function
  • Example template for adding two numbers

template ltclass Typegt Type sum (Type a, Type b)
return (a b)
Lesson 17.1
3
Function Templates
  • Called in same manner as ordinary function
  • Permissible to have both generic data types and
    ordinary data types
  • Treated similar to overloaded function
  • Need to be careful that function call data types
    compatible with function bodies
  • Useful for operations that apply to many data
    types

Lesson 17.1
4
Overloaded Function Templates
  • Cannot replace overloaded functions
  • Perform same operations for each different data
    type
  • Can vary number of arguments
  • Specify more than one type of argument
  • Distinguished by number or distribution of types
    of arguments

Lesson 17.1
5
Class Templates Example
template ltclass Typegt class Class1
private Type value public
Class1 ( ) void
set_value (Type) Type get_value
( )
Lesson 17.2
6
Class Template
  • Allows creation of object of class and use the
    data type of choice
  • Syntax to declare object
  • Class1ltdoublegt ob
  • Indicates the ob.value is type double

Lesson 17.2
7
Mechanics of Class Templates
  • Declaration for object using class template
  • Causes memory reserved for all data members
  • Causes instructions to be generated and stored
    for all function members
  • If another object with same bracketed data type
    declared
  • New memory reserved, but no new function
    instructions

Lesson 17.2
8
Friends of Class Templates
  • Four cases (assuming single type parameter)
  • Ordinary function friend of each template class
    instantiated from class template
  • Ordinary class friend of each template class
    instantiated from class template
  • Template function only if type parameter for
    function and class same
  • Template class only matching type class is
    friend

Lesson 17.2
9
Sequence Containers
Standard Template Library
  • Designed to directly control position of element
    within container
  • Three containers
  • vector
  • deque
  • list
  • Dynamic memory allocation used to reserve memory

Lesson 17.3
10
Vectors
  • Need to include ltvectorgt header vector
    ltintgt vector1
  • Declares vector1 to be vector container of int
  • Elements in contiguous memory locations
  • First element has subscript 0
  • Can be accessed using array-like notation
  • push family of functions reserve memory and
    initialize single element
  • Random access

Lesson 17.3
11
Deques
  • Need to include ltdequegt header deque
    ltchargt deque1
  • Declares deque1 to be deque container of char
  • Can be created using push_front( ) and push_back
    ( )
  • Elements in contiguous memory locations
  • Can modify values with array notation
  • First element, subscript 0
  • Random Access

Lesson 17.3
12
Lists
  • Need to include the ltlistgt header list
    ltdoublegt list1
  • Declares list1 to be list container of doubles
  • Called doubly linked list
  • Two pointer values one to next element and
    another to previous element
  • Not stored in contiguous memory

Lesson 17.3
13
Iterators
Standard Template Library
  • Designed to be user-friendly pointers
  • Know type of container
  • Can go through list with operator
  • General form for declaring container lttypegt
    iterator name
  • ordinary iterator needs no special header file

Lesson 17.4
14
Using an Iterator
  • Need to initialize to point to location first
    then manipulate
  • begin ( ) member function returns object that
    points to memory location of first element
  • Can access element pointed to by iterator using
    unary operator

Lesson 17.4
15
Constant Iterators
  • General form or declaringcontainer lttypegt
    const_iterator name

Lesson 17.4
16
List Iterators and Operators
  • Called bidirectional iterators
  • Cannot advance more than one element at a time
  • Use both and - - to more forward and backward
    in list
  • Useable operators
  • unary operator, , --, , , and !

Lesson 17.4
17
Algorithms
Standard Template Library
  • Different definition than dictionary
  • Global template functions designed to work with
    containers using iterators
  • Not member functions
  • called with function name and argument list
    name (iterator1, iterator2, iterator3)
  • name is name of algorithm
  • iterator1, iterator2, iterator3 names of
    iterators or return values from member functions

Lesson 17.5
18
Summary
  • Function templates
  • Class templates
  • Three types of sequences containers are vector,
    deque and list
  • Basic components of STL are iterators, algorithms
    and containers
  • STL has both sequence and associative containers
Write a Comment
User Comments (0)
About PowerShow.com