1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Programming in C++ - Chapter 17: Templates

18 58 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 378,75 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter 17 – Templates

Trang 2

Express 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 3

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

Trang 4

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

Trang 5

Lesson 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 6

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

Trang 7

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

Trang 8

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

Trang 9

Designed 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 10

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

Trang 11

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

Trang 12

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

Trang 13

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

Standard Template Library

Trang 14

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

Trang 15

General 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 16

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

Trang 17

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);

or return values from member functions

Lesson 17.5

Standard Template Library

Trang 18

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

Ngày đăng: 30/01/2020, 01:27

TỪ KHÓA LIÊN QUAN