Template concept

Reprinted from: http://www.360doc.com/content/09/0403/17/799_3011262.shtml

1. The concept of templates.

We have already learned about overloading (Overloading) . For overloaded functions, C++'s checking mechanism can pass the difference of function parameters and the difference of the class to which it belongs. Correctly call overloaded functions. For example, in order to find the maximum value of two numbers, we need to define different overloaded (Overload) versions for different data types when we define the MAX() function .

copy code
// function 1.

int max(int x,int y);
{return(x>y)?x:y ;}

//函数2.
float max( float x,float y){
return (x>y)? x:y ;}

//函数3.
double max(double x,double y)
{return (c>y)? x:y ;}
copy code

But if in the main function, we define char a,b; respectively, then the program will error when executing max(a,b);, because we do not define the overloaded version of char type.

Now, let's re-examine the above max() function. They all have the same function, that is, to find the maximum value of two numbers. Can we just write a set of code to solve this problem? This will avoid calling errors caused by incomplete definitions of overloaded functions. In order to solve the above problems, C++ introduces the template mechanism, template definition: template is a tool to realize the code reuse mechanism, it can realize type parameterization, that is, define the type as a parameter, thus realizing the real code reusability. Templates can be divided into two categories, one is a function template, the other is a class template.

2. Writing a function template

The general form of a function template is as follows:

Template <class or typename T>

Return type function name (formal parameter list)
{ // function definition body}

Note: template is a keyword for declaring a template, which means that a template keyword class cannot be omitted. If there is more than one type parameter, a class must be added before each parameter. <Type parameter list> can contain basic data types and can contain class type.

Please see the following procedure:

copy code
//Test.cpp

#include <iostream>

using std::cout;

using std::endl;
copy code

//Declare a function template to compare the size of two input parameters of the same data type, class can also be replaced by typename,

//T can be replaced by any letter or number.

copy code
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<< "较小整数:"<<min(n1,n2)<<endl;

     cout<< "较小实数:"<<min(d1,d2)<<endl;

     system("PAUSE");

}
copy code

Program running result:

 

Program analysis: The main() function defines two integer variables n1 , n2 two double-precision type variables d1 , d2 and then calls min( n1, n2); that is, instantiate the function template T min(T x, T y) Where T is of type int, find the minimum value among n1 and n2. Similarly, when min(d1, d2) is called, the minimum value of d1 and d2 is obtained.

3. Writing a class template

Define a class template:

Template <class or also typename T>
class class name {
//Class definition. . . . . .
};

Description: Among them, template is the keyword for declaring each template, which means declaring a template, and the template parameter can be one or more.

For example: define a class template:

copy code
// ClassTemplate.h
#ifndef ClassTemplate_HH
#define ClassTemplate_HH

template<typename T1,typename T2>

class myClass{

private:
     T1I;
     T2 J;

public:
     myClass(T1 a, T2 b);//Constructor
     void show();
};


// this is the constructor
 // note these formats 

template <typename T1,typename T2>

myClass<T1,T2>::myClass(T1 a,T2 b):I(a),J(b){}

 

//这是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");
}
copy code

The final result shows:

 

4. Non-type template parameters

In general, non-type template parameters can be constant integers (including enums) or pointers to externally linked objects.

That said, floating point numbers are no good, pointers to internally linked objects are no good.

copy code
template<typename T, int MAXSIZE>
class Stack{
Private:
       T elems[MAXSIZE];
};

Int main()
{

       Stack<int, 20> int20Stack;
       Stack<int, 40> int40Stack;

};
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643995&siteId=291194637