用 #include 格式来引用标准库的头文件

时间:2024-08-10 19:35:08

用 #include <filename.h> 格式来引用标准库的头文件(编译器将从 标准库目录开始搜索)。

 #include <iostream>

 /* run this program using the console pauser or add your own getch, system("pause") or input loop */

 using namespace std;

 //定义结构
struct student {
char name[];
float grade;
}; //交换student类型的数据
void swap(student &x,student &y) //swap的参数为引用传递方式
{
student temp;
temp=x;
x=y;
y=temp;
} //返回student类型的引用,求优者
student& max(student &x,student &y) //swap的参数为引用传递方式
{
return (x.grade>y.grade?x:y);
} //显示student类型的数据
void show(student &x) //show的参数为引用传递方式
{
cout<<x.name<<" "<<x.grade<<endl;
}
int main(int argc, char** argv) { student a={"ZhangHua",351.5},b={"WangJun",}; //显示a和b的数据
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl; //交换a和b的数据,并显示
swap(a,b);
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl; //计算和显示成绩高者
student t=max(a,b);
cout<<"Max:";
show(t);
return ;
}