INB221 Lecture Notes - Lecture 5: Insertion Sort, Linear Search, Substring

100 views5 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents