指针篇
1.如果交换两个变量值
#include<iostream>
#include<Windows.h>
using namespace std;
void main(void)
{
DWORD take = GetTickCount();
void swap1(int *,int *);
void swap2(int &,int &);
//怎样交换两个变量的值
int a,b,c,d,e,f;
a=1,b=2,c=3,d=4,e=5,f=6;
//方法1:定义两个指针
int *a1=&a,*b1=&b;
swap1(a1,b1)
//方法2:直接引用
swap1(&c,&d);
//方法3:定义引用函数
swap2(e,f);
cout<<"方法1结果:"<<endl;
cout<<"a="<<a<<"\nb="<<b<<endl;
cout<<"方法2结果:"<<endl;
cout<<"c="<<c<<"\nd="<<d<<endl;
cout<<"方法3结果:"<<endl;
cout<<"e="<<e<<"\nf="<<f<<endl;
//运行时间
cout<<"The operating time is :"<<(double)(GetTickCount()-take)/1000<<endl;
system("pause");
}
void swap1(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void swap2(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
本文来源:https://www.wddqw.com/doc/b086da3eeefdc8d376ee32ab.html
正在阅读:
C++如何交换两个变量的数值01-01
丝绸之路简介01-01
有线电视台电视节目表01-01
互联网对大学生的影响01-01
湖南专科学校排名01-01
关于猫的动画片01-01
谁是卧底01-01
读书记录卡01-01