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
18 trang |
Chia sẻ: thuongdt324 | Lượt xem: 474 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Electrical Engineering - Chapter 17: Templates, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 17 – TemplatesFunction TemplatesExpress general form for a functionExample: template for adding two numbersLesson 17.1template Type sum (Type a, Type b) { return (a + b); }Function TemplatesCalled in same manner as ordinary functionPermissible to have both generic data types and ordinary data typesTreated similar to overloaded functionNeed to be careful that function call data types compatible with function bodiesUseful for operations that apply to many data typesLesson 17.1Overloaded Function TemplatesCannot replace overloaded functionsPerform same operations for each different data typeCan vary number of argumentsSpecify more than one type of argumentDistinguished by number or distribution of types of argumentsLesson 17.1Class Templates: ExampleLesson 17.2template class Class1{ private: Type value; public: Class1 ( ); void set_value (Type); Type get_value ( );};Name of classData memberConstructorMember functionsClass TemplateAllows creation of object of class and use the data type of choiceSyntax to declare objectClass1 ob;Indicates the ob.value is type doubleLesson 17.2Mechanics of Class TemplatesDeclaration for object using class templateCauses memory reserved for all data membersCauses instructions to be generated and stored for all function membersIf another object with same bracketed data type declaredNew memory reserved, but no new function instructionsLesson 17.2Friends of Class TemplatesFour cases (assuming single type parameter)Ordinary function friend of each template class instantiated from class templateOrdinary class friend of each template class instantiated from class templateTemplate function – only if type parameter for function and class same Template class – only matching type class is friend Lesson 17.2Sequence ContainersDesigned to directly control position of element within containerThree containersvectordequelistDynamic memory allocation used to reserve memoryLesson 17.3Standard Template LibraryVectorsNeed to include header vector vector1;Declares vector1 to be vector container of intElements in contiguous memory locationsFirst element has subscript 0Can be accessed using array-like notation“push” family of functions reserve memory and initialize single elementRandom accessLesson 17.3DequesNeed to include header deque deque1;Declares deque1 to be deque container of charCan be created using push_front( ) and push_back ( )Elements in contiguous memory locationsCan modify values with array notationFirst element, subscript 0Random AccessLesson 17.3ListsNeed to include the header list list1;Declares list1 to be list container of doublesCalled doubly linked listTwo pointer values: one to next element and another to previous elementNot stored in contiguous memoryLesson 17.3IteratorsDesigned to be user-friendly pointersKnow type of containerCan go through list with ++ operatorGeneral form for declaring container :: iterator name;ordinary iterator needs no special header fileLesson 17.4Standard Template LibraryUsing an IteratorNeed to initialize to point to location first then manipulatebegin ( ) member function returns object that points to memory location of first elementCan access element pointed to by iterator using unary * operatorLesson 17.4Constant IteratorsGeneral form or declaringcontainer :: const_iterator name;Lesson 17.4Type of container such as list, vector or dequeData type of containerValid identifier for iteratorList Iterators and OperatorsCalled bidirectional iteratorsCannot advance more than one element at a timeUse both ++ and - - to more forward and backward in listUseable operatorsunary * operator, ++, --, =, ==, and !=Lesson 17.4AlgorithmsDifferent definition than dictionaryGlobal template functions designed to work with containers using iteratorsNot member functionscalled with function name and argument list name (iterator1, iterator2, iterator3);name is name of algorithmiterator1, iterator2, iterator3 names of iterators or return values from member functionsLesson 17.5Standard Template LibrarySummaryFunction templatesClass templatesThree types of sequences containers are vector, deque and listBasic components of STL are iterators, algorithms and containersSTL has both sequence and associative containers