C++中函数模板template的使用


 

下面以一个简单程序演示一下函数模板的使用:

 1 #include<iostream>
 2 using namespace std;
 3 template<class Type>
 4 Type Abc(Type a,Type b,Type c)
 5 {
 6     return a+b+c;
 7 }
 8 int main()
 9 {
10     int a=1,b=2,c=3;
11     cout<<"a= "<<a<<",b= "<<b<<",c= "<<c<<endl;
12     cout<<"使用函数模板的结果为:\n";
13     cout<<Abc(a,b,c)<<endl;
14     float f=4,d=0.6,e=2.3;
15     cout<<"f= "<<f<<",d= "<<d<<",e= "<<e<<endl;
16     cout<<"使用函数模板的结果为:\n";
17     cout<<Abc(f,d,e)<<endl;
18     return 0;
19 }

 

调试运行结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM