赞
踩
#include<iostream> using namespace std; void swap_1(int a, int b)//无法实现值交换 { int flag; flag = a; a = b; b = flag; } void swap_2(int *a, int *b)//在函数中能实现值交换,但无法返回主函数中 { int *flag; flag = a;//改变地址无法改变指针指向的内容 a = b; b = flag; cout << *a<<" " << *b << endl;; } void swap_3(int *a,int *b)//可以实现值的交换 { int flag; flag = *a; *a = *b; *b = flag; } int main() { int a, b; cin >> a >> b; swap_1(a, b); cout << a<<" " << b << endl; swap_2(&a, &b); cout << a <<" "<< b << endl; swap_3(&a, &b); cout << a << " " << b << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。