#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>头文件

评论