#include <iostream> using namespace std; int max(int num1, int num2); void swap(int *x, int *y); void swap(int &x, int &y); int main(){ cout << "hello world" << endl; int num1 = 100; int num2 = 200; int v_max = max(num1, num2); cout << v_max << endl; //cout << "before swap " << num1 << " " << num2 << endl; //swap(&num1, &num2); //cout << "after swap " << num1 << " " << num2 << endl; cout << "before swap " << num1 << " " << num2 << endl; swap(num1, num2); cout << "after swap " << num1 << " " << num2 << endl; } int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } void swap(int *x, int *y) { int temp; temp = *x; /*保存指针*/ *x = *y; *y = temp; return; } void swap(int &x, int &y) { int tmp; tmp = x; x = y; y = tmp; return; }
本文提供了一个简单的C++程序实例,演示了如何确定两个整数中的最大值,并通过两种不同的方法实现变量值的交换。该程序包括基本的函数定义和使用引用及指针进行值交换的方法。

被折叠的 条评论
为什么被折叠?



