Templates are the basis for generic programming in C + +. A template is a blueprint or formula that creates a class or function. When using generic types such as vectors or generic functions such as find, we provide enough information to convert the blue sky into a specific class or function. This conversion occurs at compile time.
Defining templates
-The two functions are almost identical, and the only difference is the type of the parameter, which is exactly the same as the function body
1 intCompareConst string&V1,Const string&v2)2 {3 if(V1 < v2)return-1;4 if(V2 < V1)return 1;5 return 0;6 }7 intCompareConst Double&V1,Const Double&v2)8 {9 if(V1 < v2)return-1;Ten if(V2 < V1)return 1; One return 0; A}
-"function template
-"We can define a generic function template instead of defining a new function for each type, a function template is a formula that can be used to generate a version of a function for a particular type." The compare version of the function may look like this
Template <typename t> // template definition keyword template parameter list <typename T,...; In the template definition, the Templates argument list cannot be empty int Compare (constconst T &v2) { ifreturn - 1 ; if (V2 < V1) return 1 ; return 0 ;}
Write here feel good tired, tomorrow in writing ...
C + + templates and generics programming