正文

c++中模板使用的一个小例子2009-10-18 10:10:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bigpig/49381.html

分享到:

#include<iostream.h>#include<iomanip.h> /*声明模板函数*/template<class T> void Swap(T &x,T &y);/*   Write :2009年10月17日  */ /*定义结构体数据类型*/struct STU{ int number; char name[3]; float score;};int main(){ /*定义两个成员变量*/ STU s1={1001,"s1",90}; STU s2={1002,"s2",89}; /*模板的引用*/ Swap(s2,s1); cout<<"s1:"<<setw(6)<<s1.number<<setw(6)<<s1.name<<setw(6)<<s1.score<<endl; cout<<"s2:"<<setw(6)<<s2.number<<setw(6)<<s2.name<<setw(6)<<s2.score<<endl; cin.get(); return 0;} /*定义模板函数*/template <class T> void Swap(T &x,T &y){ T temp; temp=x; x=y; y=temp;} 注意:在使用setw()函数定义输出格式时要在开始添加#include<iomanip.h>头文件

阅读(2826) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册