1. divergence Problem: A method of several exchange variables in C + +
(1) macro code block VS function
Exchange of "programming experiment" variables
#include <iostream>#include<string>using namespacestd;//macro Definition code block#defineSWAP (T, a, B) Do{T C=A; A=b; b=C; } while(0);//define function ModevoidSwap (int& A,int&b) { intc =A; A=b; b=C;}voidSwap (Double& A,Double&b) { Doublec =A; A=b; b=C;}voidSwap (string& A,string&b) { stringc =A; A=b; b=C;}intMain () {intA =0; intb =1; Swap (A, b); //Swap (int&, int&)cout<<"A ="<< a <<Endl; cout<<"B ="<< b <<Endl; SWAP (int, A, b);//Macro Codecout<<"A ="<< a <<Endl; cout<<"B ="<< b <<Endl; Doublem =2; Doublen =3; Swap (M, n); //Swap (double&, double&)cout <<"m ="<< m <<Endl; cout<<"n ="<< N <<Endl; SWAP (Double, M, n);//Macro Codecout <<"m ="<< m <<Endl; cout<<"n ="<< N <<Endl; strings ="Santa"; stringc ="Claus"; Swap (S, c); cout<<"s ="<< s <<Endl; cout<<"C ="<< C <<Endl; SWAP (string, S, c); cout<<"s ="<< s <<Endl; cout<<"C ="<< C <<Endl; return 0;}
(2) Comparison of advantages and disadvantages
|
Hong Yihong code block |
Defining functions |
Advantages |
code reuse for all types |
A real function call , the compiler checks the type |
Disadvantages |
compiler does not know the existence of macros, missing type checking |
Duplicate a function by type, cannot code reuse |
2. New Solution-Generic programming
(1) Regardless of the specific data types of programming, the combination of the advantages of the above two scenarios.
(2) function template: Generics in C + +
① a special function can be called with different types
② looks similar to normal functions, except that types can be parameterized , such as
Template<typename t>
void Swap (t& A, t& B)
(3) Syntax rules for function templates
The ①template keyword is used to declare the beginning of generic programming
②TypeName keyword for declaring a generic type
3. Use of function templates
(1) automatic type deduction call
int a = 0, b = 1;
Swap (A, b); Automatic derivation
(2) Explicit invocation of a specific type
float c = 2, d = 3;
Swap<float> (c, D); Explicitly called
On the use of "programming experiment" function template
#include <iostream>#include<string>using namespacestd;template<typename t>voidSwap (t& A, t&b) {T C=A; A=b; b=C;} Template<typename t>voidSelectsort (T a[],intLen) { for(inti =0; i < Len; i++) { for(intj = i; J < Len; J + +) { if(A[i] < a[j])//Descending{Swap (a[i], a[j]); }}}}template<typename t>voidPrintln (T a[],intLen) { for(inti =0; i < Len; i++) {cout<< A[i] <<", "; } cout<<Endl;}intMain () {inta[5] = {5,3,2,4,1}; Println (A,5); Selectsort (A,5); Println (A,5); strings[5] = {"Java","C + +","Pascal","Ruby","Basic"}; Println (s),5); Selectsort (s),5); Println (s),5); return 0;}
4. Summary
(1) function template is one of the ways that generic programming is applied in C + +
(2) function template can deduce parameter types based on actual parameters
(3) Function templates support explicitly specifying parameter types
(4) Function template is an important method of code reuse in C + +
The concept and significance of function template in the 56th lesson