On completion of this chapter students will know how to: 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.
Trang 1Chapter 17 – Templates
Trang 2Express general form for a function
Example: template for adding two numbers
Lesson 17.1
template <class Type>
Type sum (Type a, Type b) {
return (a + b);
}
Trang 3Called 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
Trang 4Cannot 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
Trang 5Lesson 17.2
template <class Type>
class Class1 {
private:
Type value;
public:
Class1 ( );
void set_value (Type); Type get_value ( );
};
Name of class
Data member Constructor
Member functions
Trang 6Allows creation of object of class and use the data type of choice
Syntax to declare object
– Class1<double> ob;
– Indicates the ob.value is type double
Lesson 17.2
Trang 7Declaration 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
Trang 8Four 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
Trang 9Designed to directly control position of element within container
Three containers
– vector – deque – list
Dynamic memory allocation used to reserve memory
Lesson 17.3
Standard Template Library
Trang 10Need to include <vector> header
vector <int> vector1;
– Declares vector1 to be vector container of int
Elements in contiguous memory locations
– First element has subscript 0
Can be accessed using arraylike notation
– “push” family of functions reserve memory and initialize single element
Random access
Lesson 17.3
Trang 11Need to include <deque> header
deque <char> 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
Trang 12Need to include the <list> header
list <double> 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
Trang 13Designed to be userfriendly pointers
– Know type of container
Can go through list with ++ operator General form for declaring
container <type> :: iterator name;
ordinary iterator needs no special header file
Lesson 17.4
Standard Template Library
Trang 14Need 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
Trang 15General form or declaring
container <type> :: const_iterator name;
Lesson 17.4
Type of container such as list, vector or deque
Data type of container
Valid identifier for iterator
Trang 16Called 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
Trang 17Different 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);
or return values from member functions
Lesson 17.5
Standard Template Library
Trang 18Function 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