This article is based on the interpretation of Self-Internet-related templates, and then written after self-practice and understanding. Thanks for the help of Internet materials. Template definition:A template is a tool for implementing the code reuse mechanism. It can implement type parameterization, that is, define the type as a parameter, thus realizing the real code reusability. Templates can be divided into two types: function templates and class templates. Template implementation writing method:1. Writing function templatesThe function template is generally in the following format:Template <ClassOr you can useTypenameT>Return type function name parameter table)
{// Function Definition body}Description: template is a keyword used to declare a template. It indicates that the class that declares a template keyword cannot be omitted. If there are more than one type parameter, class <type parameter table> must be added before each parameter. It can contain basic data types and class types.Note:"Template <ClassOr you can useTypenameT>.// No other statements are allowed between the template statement and the function template statement. See the following program:// Mian. cpp# Include <iostream> // declare a function template to compare the input values of two parameters of the same data type. The class can also be replaced by typename, // T can be replaced by any letter or number. Template <class T> T max (T&X, T&Y) // it must be a parameter reference and cannot be T min (T x, T y) {if (a> B)
{
Return;
}
Return B;} void main () {int i1 = 3, i2 = 5;
Char c1 = 'A', c2 = 'B ';
Double d1 = 0.35, d2 = 0.2;
Cout <"for int:" <max (i1, i2) <endl;
Cout <"for char:" <max (c1, c2) <endl;
Cout <"for double:" <max (d1, d2) <endl ;}2.Class template writingDefine a class template:Template <ClassOr you can useTypenameT>Class name {
};Note: "template" declares the keywords of each template, indicating that a template can be declared. The template parameter can be either a template or multiple templates. EachClass or typename, separated by commas (,).Note:"Template <Class Or you can useTypenameT>"This line.For example, define a class template:// Template. h
# Paragm onceTemplate <typename T1, typename T2>Class myClass {private: T1 I; T2 J; public: myClass (T1 a, T2 B );//If a template is used in a member function, this function must be defined in vitro !!! Void show () ;}; // constructor template <typename T1, typename T2> myClass <T1, T2 >:: myClass (T1 a, T2 B ): I (a), J (B) {}void show ();Template <typename T1, typename T2>VoidMyClass <T1, T2> ::Show () {cout <"I =" <I <", J =" <J <endl ;}Note: The template declaration must be added before the member functions of each template class.Template <typename T1, typename T2>, and then the name is similar to a common class, but the class parameter must be added.MyClass <T1, T2> // Run. cpp# Include "template. h "# include <iostream> using namespace std; void main () {// What is the object of a class similar to a common Class Object declaration, but must instantiate class template parameters, such: myClass <int, int>, the rest of the call function methods are the same as normal classesMyClass <int, int>Class1 (1, 2 );
Class1.show (); myClass <char, char> class2 ('A', 'B ');
Class2.show ();}3. Non-type template parameters
Generally, a non-type template parameter can be a constant integer including enumeration) or a pointer to an external link object. That is to say, floating point numbers cannot be used, and pointers to internal linked objects cannot be used.
Template <typename T, int MAXSIZE> class Stack {Private: T elems [MAXSIZE];… }; Int main () {Stack <int, 20> int20Stack; Stack <int, 40> int40Stack ;... };Note: the most important thing is to pay attention to the format, especially the format defined by the class template and the format of the instantiated class template. when defining a function template, parameters are referenced &!