#include <iostream>
#include <string>
using namespace std;
//值,指针,引用
//两个数比较最大的输出
//三个数比较最大的输出
int max(int a,int b);//值
int max1(int *a,int *b);//值
int &max2(int &a,int &b);//值
int main()
{
int a,b,c;
cin>>a>>b>>c;
//cout<<max(max(a,c),b)<<endl;
cout<<max2(max2(a,c),b)<<endl;
return 0;
}
int max(int a,int b)
{
if(a>b)return a;
else return b;
}
int max1(int *a,int *b)//指针传递
{
if(*a>*b)return *a;
else return *b;
}
int &max2(int &a,int &b)//引用传递
{
if(a>b)return a;
else return b;
}
C++比大小(指针传递,值传递)
最新推荐文章于 2024-07-04 14:36:31 发布
该博客介绍了如何在C++中使用值、指针和引用来比较并输出三个数中的最大值。分别通过`max`函数的三种实现方式——值传递、指针传递和引用传递,展示了不同参数传递方式在比较最大值时的用法。在`main`函数中,对三个输入的整数进行了比较并输出了最大值。
摘要由CSDN通过智能技术生成