STL _ FUNCTION template, stl function Template
# Include <iostream>
# Include <string>
Using namespace std;
# Define MAX (T )\
T max _ # T (T x, T y ){\
Return x> y? X: y ;\
}
// Generate the following three types of functions during preprocessing
MAX (int) // int max_int (int x, int y) {return x> y? X: y ;}
MAX (double)
MAX (string)
# Define mymax (T) max _ # T // The function name is replaced by a macro function
Int main ()
{
Cout <mymax (int) (100,200) <endl; // max_int (100,200)
Cout <mymax (double) (1.23, 4.56) <endl;
Cout <mymax (string) ("hello", "world") <endl;
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
// Function Template
Template <typename T>
T mymax (T x, T y)
{
Return x> y? X: y;
}
Int main ()
{
Cout <mymax <int> (100,200) <endl;
Cout <mymax <double> (1.23, 4.56) <endl;
Cout <mymax <string> ("hello", "world") <endl;
Return 0;
}