正在加载图片...
/ Fig. 14.1: fig14_01. cpp 2 // Using template functions 3 #include <iostream 4 using std: cout 函数模板的参数T必须在模板头 5 using std: end1 中声明 7// function template printArray definition 8 template< typename t> 9 void printArray c const T *array, int count D 10 for c int i =0: i count; i++) 12 cout < array[ i]<< 13 14 cout < endl 如果是用户自定义类型,必须 15]// end function template printArray 要对<<运算符进行重载 16 17 int main o 18{ 19 const int ACOUNT 5: / size of array a 20 const int BCOUNT 7;// size of array b const int CCoUNT 6:// size array c 22 23inta[ ACOUNT]={1,2,3,4,5}; double b[ BCOUNT]={1.1,2.2,3.3,4.4,5.5,6.6,7.7}; 25 char c[ CCOUNT ]=HELLo";// 6th position for null 26 cout < Array a contains:<< endT 0 2018, SEU. All rights reserved. 12© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 12 1 // Fig. 14.1: fig14_01.cpp 2 // Using template functions. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 // function template printArray definition 8 template< typename T > 9 void printArray( const T *array, int count ) 10 { 11 for ( int i = 0; i < count; i++ ) 12 cout << array[ i ] << " "; 13 14 cout << endl; 15 } // end function template printArray 16 17 int main() 18 { 19 const int ACOUNT = 5; // size of array a 20 const int BCOUNT = 7; // size of array b 21 const int CCOUNT = 6; // size of array c 22 23 int a[ ACOUNT ] = { 1, 2, 3, 4, 5 }; 24 double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 25 char c[ CCOUNT ] = "HELLO"; // 6th position for null 26 27 cout << "Array a contains:" << endl; 函数模板的参数T必须在模板头 中声明 如果是用户自定义类型,必须 要对<<运算符进行重载
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有