Navigation
1. Function template and precautions
2. Write a sort function with the template
3. Difference between ordinary function and function template
4. Call of common function and function template
5. Limitations of formwork
-----------------—
1. Function template
Syntax: template < typename T >
typename can also use class
Where T is a general type, here are two examples
Example 1:
#include <iostream> using namespace std; //Exchange two integer functions void swapint(int &a,int &b) { int temp = a; a = b; b = temp; } //Swap two floating-point functions void swapDouble(double &a,double &b) { double temp = a; a = b; b = temp; } //Write two functions and call again void test01() { int a = 10,b = 20; double c = 1.1,d = 2.2; swapint(a,b); swapDouble(c,d); cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; cout<<"c = "<<c<<endl; cout<<"d = "<<d<<endl; } int main() { test01(); system("pause"); return 0; }
In order to exchange two different types of values, we need to write two functions. If the string type is exchanged, we need to write another function. In this case, we can use the template
Example 2:
#include <iostream> using namespace std; //Function template template<typename T> void swapT(T &a,T &b) { T temp = a; a = b; b = temp; } void test02() { int a = 10,b = 20; double c = 1.1,d = 2.2; //1. Automatic type derivation swapT(a,b); //2. Display the specified type swapT<double>(c,d); cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; cout<<"c = "<<c<<endl; cout<<"d = "<<d<<endl; } int main() { test02(); system("pause"); return 0; }
Here we can write a template application. There are two methods for template application
1. Automatic type derivation - (swap(a,b))
2. Display the specified type - (swap < int > (a, b))
Matters needing attention
1. Automatic derivation type. You must derive consistent data type before using
2. The template must determine the data type of T before it can be used
Example 1: (Note 1)
#include <iostream> using namespace std; //Function template template<typename T> void swapT(T &a,T &b) { T temp = a; a = b; b = temp; } //Matters needing attention void test() { int a = 10; int b = 20; char c = 'a'; swapT(a,b); //Correct //Swap (a, c); / / error } int main() { test(); system("pause"); return 0; }
Example 2: (Note 2)
#include <iostream> using namespace std; //Function template template<typename T> void func() { cout<<"func()call"<<endl; } //Matters needing attention void test() { func<void>(); //Data type must be added here, otherwise it cannot be called } int main() { test(); system("pause"); return 0; }
-----------------—
2. Using template to write sorting function
#include <iostream> using namespace std; //Sorting arrays of different data types //From large to small, the algorithm is selective sorting //Type char, int template<class T> //Exchange value template void myswap(T &a,T &b) { T temp = a; a = b; b = temp; } //Sorting algorithm template template<class T> void mysort(T arr[],int len) { for(int i=0;i<len;i++) { int max = i; for(int j=i+1;j<len;j++) { if(arr[j]>arr[max]) { max = j; } } if(max != i) { //Exchange max and i Subscripts myswap(arr[i],arr[max]); } } } //Provide print array template template<class T> void myprintArr(T arr[],int len) { for(int i=0;i<len;i++) { cout<<arr[i]<<" "; } } void test01() { //Test char array char charArr[] = "badcfe"; int len = sizeof(charArr)/sizeof(char); mysort(charArr,len); myprintArr(charArr,len); } void test02() { //Test int array int intArr[] = {7,2,5,4,3,1}; int len = sizeof(intArr)/sizeof(int); mysort(intArr,len); myprintArr(intArr,len); } int main() { test02(); system("pause"); return 0; }
It's easier to write with templates
-----------------—
3. Difference between ordinary function and function template
1. Implicit type conversion can occur in ordinary function calls
2. Function template is derived by automatic type, and implicit type conversion is not allowed
3. Function template uses display to specify type, and implicit type conversion can occur
Example:
#include <iostream> using namespace std; //Ordinary function int func(int a,int b) { return a+b; } //Call template template<class T> T fun(T a, T b) { return a+b; } void test02() { int a =10,b =20; char c = 'a'; //1. Implicit type conversion can occur when calling ordinary functions cout<<func(a,b)<<endl; cout<<func(a,c)<<endl; //2. Automatic type derivation with function template without implicit type conversion cout<<fun(a,b)<<endl; //3. The function template uses display to specify the type, and implicit conversion can occur cout<<fun<int>(a,c)<<endl; //The specified type is provided here } int main() { test02(); system("pause"); return 0; }
-----------------—
4. Call rules of common functions and function templates
1. If both the function template and the ordinary function can be called, the ordinary function will be called first
2. The function template can be forcibly called through the empty template parameter list
3. Function templates can also be overloaded
4. If the function template can produce a better match, call the function template with limited calls
Call rule 1
#include <iostream> using namespace std; //Ordinary function void myprint(int &a,int &b) { cout<<"Call normal function"<<endl; } //Function template template<class T> void myprint(T &a,T &b) { cout<<"Function template call"<<endl; } void test() { int a=10,b=20; myprint(a,b); } int main() { test(); system("pause"); return 0; }
The result is to call the normal function first
Call rule 2
#include <iostream> using namespace std; //Ordinary function void myprint(int &a,int &b); //{ // Cout < < call ordinary function < < endl; //} //Function template template<class T> void myprint(T &a,T &b) { cout<<"Function template call"<<endl; } void test() { int a=10,b=20; myprint<>(a,b); //Force call with empty template parameter list } int main() { test(); system("pause"); return 0; }
Call rule 3
If the function template is overloaded, the calling parameters are different.
Call Rule 4
#include <iostream> using namespace std; //Ordinary function void myprint(int a,int b) { cout<<"Call normal function"<<endl; } //Function template template<class T> void myprint(T a,T b) { cout<<"Function template call"<<endl; } void test() { char a = 'a',b = 'c'; myprint(a,b); } int main() { test(); system("pause"); return 0; }
Function template will be called here
-----------------—
5. Limitations of formwork
If you only pass in some single data of common type, you can. If you pass in an array or a class, you need to implement it with concrete methods
Example:
#include <iostream> using namespace std; #include <string> //Write a class class Person { public: Person(string name,int age) { this->name = name; this->age = age; } string name; int age; }; //Template is used for a single common type, not for array or customization. It should be implemented in a specific way template<class T> bool compare(T a,T b) { if(a==b) { return true; } else { return false; } } //Write a template of person class, and the incoming template should also be changed to the corresponding type template<> bool compare(Person p1,Person p2) { if(p1.name == p2.name && p1.age == p2.age) { return true; } else { return false; } } void test() { int a=10,b =10; bool ret = compare(a,b); //Test common types if(ret) { cout<<"a == b"<<endl; } else { cout<<"a != b"<<endl; } //Test the Person class Person p1("Xiao Ming",12); Person p2("Xiao Ming",12); bool ret1 = compare(p1,p2); if(ret1) { cout<<"p1 == p2"<<endl; } else { cout<<"p1 != p2"<<endl; } } int main() { test(); system("pause"); return 0; }