From:http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html
1. the concept of templates.
We have learned overload (overloading), for overloaded functions, the check mechanism of C + + can be different from the function parameter and the owning class. Call the overloaded function correctly. For example, to find the maximum value of two numbers, we define the max () function to define different overloaded (overload) versions of different data types separately.
Function 1.
int max (int x,int y);
{return (x>y)? X:y;}
Function 2.
Float max (float x,float y) {
Return (x>y)? X:y;}
Function 3.
Double Max (double x,double y)
{return (c>y)? X:y;}
But if in the main function, we define a char, a, a, respectively; Then, when you execute Max (A, b), the program will go wrong because we do not have an overloaded version of the char type defined.
Now, we re-examine the max () function above, they all have the same function, that is, to find the maximum value of two, can write a set of code to solve the problem? This avoids the invocation error caused by the overloaded function definition. In order to solve the above problems, C + + introduces template mechanism, template definition: template is a tool to implement code reuse mechanism, it can implement type parameterization, that is, the type is defined as a parameter, thus realizing the real code reusability. Template can be divided into two categories, one is a function template, the other is a class template.
2. How function templates are spelled
The general form of a function template is as follows:
Template <class or you can use typename t>
return type function name (formal parameter list)
{//function definition Body}
Description: Template is a keyword that declares templates, which declares that a template keyword class cannot be omitted, and if the type parameter is extra, the class < type parameter list > can contain the base data type can contain the class type, before each parameter.
Please see the following procedure:
Test.cpp
#include <iostream>
Using Std::cout;
Using Std::endl;
Declare a function template to compare the size of the parameters of the input two of the same data type, class can be replaced by TypeName,
T can be replaced by any letter or number.
Template <class t>
T min (t x,t y)
{return (x<y)? X:y;}
void Main ()
{
int n1=2,n2=10;
Double d1=1.5,d2=5.6;
cout<< "Small Integer:" <<min (N1,N2) <<endl;
cout<< "Smaller real numbers:" <<min (D1,D2) <<endl;
System ("PAUSE");
}
3. how class templates are written
Define a class Template:
Template < class or you can also use typename T >
Class Name {
Class definition ...
};
Description: Where the template is a keyword that declares templates, it declares a template, the template parameter can be one, or it can be multiple.
For example, define a class template:
ClassTemplate.h
#ifndef CLASSTEMPLATE_HH
#define CLASSTEMPLATE_HH
Template<typename T1,typename t2>
Class myclass{
Private
T1 I;
T2 J;
Public
MyClass (T1 A, T2 b);//constructor
void Show ();
};
This is a constructor
Note these formats
Template <typename T1,typename t2>
Myclass<t1,t2>::myclass (T1 a,t2 B): I (a), J (b) {}
This is void show ();
Template <typename T1,typename t2>
void Myclass<t1,t2>::show ()
{
cout<< "i=" <<I<< ", j=" <<J<<endl;
}
#endif
Test.cpp
#include <iostream>
#include "ClassTemplate.h"
Using Std::cout;
Using Std::endl;
void Main ()
{
myclass<int,int> Class1 (3,5);
Class1.show ();
Myclass<int,char> Class2 (3, ' a ');
Class2.show ();
Myclass<double,int> CLASS3 (2.9,10);
Class3.show ();
System ("PAUSE");
}
4. Non-type template parameters
In general, a non-type template parameter can be a constant integer (including an enumeration) or a pointer to an externally linked object.
That is, floating-point numbers do not work, and pointers to internal linked objects are not possible.
Template<typename T, int maxsize>
Class stack{
Private:
T Elems[maxsize];
...
};
Int Main ()
{
Stack<int, 20> Int20stack;
Stack<int, 40> Int40stack;
...
};
5. Using the template type
Sometimes the template type is a container or a class, to use the type under that type can be called directly, the following is a printable STL in the order and chain of the container template function
Template <typename t>
void print (T v)
{
T::iterator Itor;
for (Itor = V.begin (); Itor! = V.end (); ++itor)
{
cout << *itor << "";
}
cout << Endl;
}
void Main (int argc, char **argv) {
List<int> l;
L.push_back (1);
L.push_front (2);
if (!l.empty ())
Print (l);
Vector<int> VEC;
Vec.push_back (1);
Vec.push_back (6);
if (!vec.empty ())
Print (VEC);
}
C + + templates