C + + templates
Templates are the basis of generic programming, which is to write code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or function. Storage converters, such as iterators and algorithms, are examples of generic programming that use the concept of templates.
Each container has a single definition, such as vectors, we can define many different types of vectors, such as vector <int> or vector <string>.
You can use templates to define functions and classes, and then let's take a look at how to use them.
1#include <iostream>2 3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 using namespacestd;5 6 7 intMainintargcChar**argv) {8 intMax_value (intXintmax);9 inti,j,row=0, colum=0, Max;Ten inta[3][4]={{5, A, at, About},{ +, -,Panax Notoginseng, $},{- A,- the,6,8}}; Onemax=a[0][0]; A for(i=0; i<=2; i++) - for(j=0; j<=3; j + +) - { themax=Max_value (A[i][j],max); - if(max==A[i][j]) - { -row=i; +colum=J; - } + } Acout <<"max="<<max<<"/row="<<row<<", colum="<<colum<<Endl; at return 0; - } - - intMax_value (intXintmax) - { - if(X>max)returnx; in Else returnMax; -}
C + + templates