Function template, function template overload, variable parameter template, function template override, data exchange through reference
Zookeeper
1. function templates are beginner. If you want to use a template, You need to instantiate it. The instantiation method is to add <数据类型>
# Include
// Function templates can be used to optimize and reload types, which will be overwritten by type
// If you still want to use the template function, you need to instantiate it.
Template
Tadd (Ta, Tb)
{
Std: cout <"T add" <
Returna + B;
}
Intadd (inta, intb)
{
Std: cout <"int add" <
Returna + B;
}
Voidmain ()
{
Inta = 10, B = 20;
Doubledb1 = 10.9, db2 = 10.8;
Add (db1, db2 );
Add (a, B );
// Added It is equivalent to instantiation. The template is called.
Add (A, B );
Std: cin. get ();
}
The running result is as follows:
2. template reload. The template reload will automatically match according to the data type. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kcjxwiggfsawdupq = "left"> # include
# Include
Usingstd: array;
Template
Voidshowarray (array Myarray, intn)
{
Usingnamespacestd;
Cout <"TTTTT" <
For (inti = 0; I
{
Cout <
}
Cout <
}
Template
Voidshowarray (array Myarray, intn)
{
Usingnamespacestd;
Cout <"T *" <
For (inti = 0; I
{
Cout <* myarray [I] <"";
}
Cout <
}
Voidmain ()
{
Array Intarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Array Pintarray;
For (inti = 0; I <10; I ++)
{
Pintarray [I] = & intarray [I];
}
Array Ppintarray;
For (inti = 0; I <10; I ++)
{
Ppintarray [I] = & pintarray [I];
}
Showarray (intarray, 10 );
Showarray (pintarray, 10 );
Showarray (ppintarray, 10 );
Std: cin. get ();
}
The running result is as follows:
3. Variable Parameter templates for common functions
# Include
// The General Variable Parameter template processes an unlimited number of parameters and processes different types of parameters
// Empty function, interface, used to end recursion applies to the new version of the Compiler
Voidshowall (){}
Template
Voidshowall (constT & value, constArgs &... args)
{
Std: cout <value <
Showall (args...); // continue to pass
}
// The design can modify the T & value, Args &... args of the original data
// The design can modify the copy T value, Args... args
// The original data cannot be modified in the design, and the copy const T value and const Args... args cannot be modified.
// The original data referenced by the design cannot be modified by const T & value, const Args &... args
Voidmain ()
{
Intnum1 = 10, num2 = 9, num3 = 11;
Doubledb1 = 10.8, db2 = 10.9;
Charstr [40] = "yincheng ";
Charch = 'a ';
Showall (num1 );
Std: cout <"\ n ";
Showall (num1, num2, num3 );
Std: cout <"\ n ";
Showall (db1, db2, num1, ch );
Std: cout <"\ n ";
Showall (db1, db2, num1, ch, str );
Std: cin. get ();
}
Run the following command:
4. Function template coverage and parameter swapping
# Include
// The function template is universal and can be optimized based on its own data type.
// The struct and class can be directly initialized only when there is no private variable.
// All members are public types and can be assigned values for initialization.
Structinfo
{
Charname [40];
Doubledb;
Intdata;
};
Template
Voidswap (T & a, T & B)
{
Std: cout <"General function template" <std: endl;
Ttemp =;
A = B;
B = temp; // exchange variable
}
// The template is empty, the parameter type is specified, and the type of the function template is overwritten.
// Template <> // optional
Voidswap (info & info1, info & info2)
{
Std: cout <"special function template" <std: endl;
// You can use a template to achieve general purpose and optimize your own data types.
Infotemp = info1;
Info1 = info2;
Info2 = temp;
}
Voidmain ()
{
Infoinfo1 = {"tuzuoquan", 20.9, 10 };
Infoinfoo2 = {"quanzuotu", 9.2, 1 };
Swap (info1, info2 );
Std: cout <info1.name <info1.db <info2.data <std: endl;
Std: cout <info2.name <info2.db <info2.data <std: endl;
Std: cin. get ();
}
Running result:
5. Implement parameter swapping by referencing
# Include
// The function template is universal and can be optimized based on its own data type.
// The struct and class can be directly initialized only when there is no private variable.
// All members are public types and can be assigned values for initialization.
Template
Voidswap (T & a, T & B)
{
Std: cout <"General function template" <std: endl;
Ttemp =;
A = B;
B = temp; // exchange variable
}
Voidmain ()
{
Intnum1 = 100;
Intnum2 = 10;
Swap (num1, num2); // exchange
Std: cout <num1 <"" <
Char character = 'Z ';
Char ch2 = 'a ';
// Note: You must specify the template to use. <类型名称>
Swap (Ch2 );
Std: cout <condition <"" <
Std: cin. get ();
}
6. Variable Parameter templates
# Include
# Include
Voidshowall () {}// reserve one
Template
Voidshow (Tt ,...)
{
Std: cout <t <
}
Template
Voidshowall (Tt, Args... args)
{
Std: cout <t <
Showall (args ...);
}
Voidmain ()
{
Intnum1 = 10, num2 = 9, num3 = 11;
Doubledb1 = 10.8, db2 = 10.9;
Charstr [40] = "yincheng ";
Charch = 'a ';
Show (num1 );
Showall (num2, num3 );
Showall (num2, num3, num1, str, ch );
Std: cin. get ();
}
The running result is as follows: