赞
踩
参考:面向对象程序设计及C++,P24
- // excise_20230807d.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- using namespace std;
- void swapY(int &x, int &y) //交换,使用引用方式
- {
- int t = x;
- x = y;
- y = t;
- }
- void swapP(int* x, int* y) //交换,使用指针方式
- {
- int t = *x;
- *x = *y;
- *y = t;
- }
-
- int main()
- {
- int a = 3, b = 5, c = 10, d = 20;
- cout << "a = " << a
- << " b = " << b << endl;
- swapY(a, b); //相当于执行了int &x=a, &y = b
- cout<< "a = " << a
- << " b =" << b << endl;
-
- cout<< "c = " << c
- << " d = " << d << endl;
- swapP(&c, &d);
- cout << "c = " << c
- << " d = " << d << endl;
-
- std::cout << "Hello World!\n";
- getchar();
- }
-
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。