INB221 Lecture Notes - Lecture 5: Insertion Sort, Linear Search, Substring
Document Summary
Templates are a powerful feature which allows you to define functions and define classes. Last week we looked at intvector, intstack, intqueue. Writing an implementation for every container of every type would be time consuming. Instead we can make a template which takes a generic type, to allow you to put any type into it. The common form of template spec is "template ", placeholder being used through the template and when used properly, will be replaced with the proper datatype. Templates can be used before a function definition to create a generic colection of functions. template Valuetype maximum(valuetype v1, valuetype v2) { return (v1 > v2) ? v1 : v2; a template for a function which will find the return the bigger type. The compiler generates the different code for the different versions of maximum, and would only then be able to used on types that you can use the > operator.