正在加载图片...
Example: displayarray cpp template <typename T> void display (t array [l, int num)i for (int i =0; i< num; 1++ cout array[i] < i cout < endl oub1ex[]={1.1,2 display <double> created display(x, 5)i int num[]={1,2,3,4}; display(num, 4)i display<int> created 12.23.34.45 1234 Function-template specializations are generated automatically by the compiler to handle each type of call to the function template If an array of user-defined objects is used, need to overload < operator of the object class COMP 152Example: displayarray.cpp ▪ Function-template specializations are generated automatically by the compiler to handle each type of call to the function template ▪ If an array of user-defined objects is used, need to overload << operator of the object class. COMP152 14 template <typename T> void display(T array[], int num) { for (int i = 0; i < num; i++) cout << array[i] << " "; cout << endl; } int main() { double x[] = {1.1, 2.2, 3.3, 4.4, 5.5}; display(x, 5); int num[] = {1, 2, 3, 4}; display(num, 4); } 1.1 2.2 3.3 4.4 5.5 1 2 3 4 display<double> created display<int> created
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有