Tải bản đầy đủ (.pdf) (18 trang)

Lecture Programming in C++ - Chapter 17: Templates

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (378.75 KB, 18 trang )

Chapter 17 – Templates


Function Templates
Express general form for a function
Example:  template for adding two numbers
template <class Type>
Type sum (Type a, Type b)
     {
     return (a + b);
     }
Lesson 17.1


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


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


Class Templates: Example
template  <class Type>
class Class1 Name of class
{
       private:
             Type value; Data member
       public:
             Class1 ( ); Constructor
             void   set_value  (Type);
Member functions
             Type  get_value  ( );
};
Lesson 17.2


Class Template
Allows 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



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


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


Standard Template Library


Sequence Containers
Designed to directly control position of 
element within container
Three containers
– vector
– deque
– list

Dynamic memory allocation used to reserve 
memory
Lesson 17.3


Vectors
Need 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 array­like notation
– “push” family of functions reserve memory and 
initialize single element

Random access
Lesson 17.3



Deques
Need 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


Lists
Need 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


Standard Template Library


Iterators
Designed to be user­friendly 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


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


Constant Iterators
General form or declaring
container <type> :: const_iterator name;
Type of container such as list, vector or deque
Valid identifier for iterator
Data type of container


Lesson 17.4


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


Standard Template Library

Algorithms
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



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



×