c++ 交换两结构体变量的值

来源:互联网 发布:怎样查看淘宝店铺等级 编辑:程序博客网 时间:2024/04/29 15:03

使用vc++6.0;

#include<iostream.h>
#include<string>
using namespace std; //不加此句,string无法使用。string是定义在名字空间std中的
struct Player
{
 string name;
 int age;
 int height;
};


void main(){
       Player p1;
        Player p2;

/* vc6.0不给力,Player p1 = {"han",28,186};出现错误:

non-aggregates cannot be initialized with initializer list

*/
     p1.name = "han";
     p1.age = 28;
     p1.height = 186;
     p2.name = "wang";
     p2.age = 26;
      p2.height = 178;

     void swap(Player *a,Player *b);

     Player *pp1 = &p1;
     Player *pp2 = &p2;
     swap(pp1,pp2);
     cout<<p1.age<<endl;
}


void swap(Player *a,Player *b)
{
    Player temp;
     temp = *a;
       *a = *b;
     *b = temp;
}

原创粉丝点击