c++ 编写函数返回两个值最小值_如何从C或C++中的函数返回多个值?

95f3e80a6f98ece77b3e1f3034f39554.png

新程序员通常在寻找从函数返回多个值的方法。不幸的是,C和C++不允许直接这样做。但是幸运的是,通过一些巧妙的编程,我们可以轻松实现这一目标。

下面是从C函数中返回多个值的方法

  • 通过使用指针。
  • 通过使用结构。
  • 通过使用数组。

示例:考虑一个示例,其中的任务是查找两个不同数字中的较大和较小值。 我们可以编写多个函数。主要问题是调用多个函数的麻烦,因为我们需要返回多个值,并且当然要键入更多的代码行。

1)使用指针返回多个值:传递参数及其地址,并使用指针更改其值。

// Modified program using pointers #include    // add is the short name for address void compare(int a, int b, int* add_great, int* add_small) {     if (a > b) {           // a is stored in the address pointed         // by the pointer variable *add_great         *add_great = a;         *add_small = b;     }     else {         *add_great = b;         *add_small = a;     } }   // Driver code int main() {     int great, small, x, y;       printf("Enter two numbers: ");     scanf("%d%d", &x, &y);       // The last two arguments are passed     // by giving addresses of memory locations     compare(x, y, &great, &small);     printf("The greater number is %d and the"           "smaller number is %d",            great, small);       return 0; } 

输出

Enter two numbers: 5 8The greater number is 8 and the smaller number is 5

2)使用结构返回多个值:由于结构是用户定义的数据类型。这个想法是用两个整数变量定义一个结构,并将较大和较小的值存储到这些变量中,然后使用该结构的值。

// Modified program using structures #include  struct greaterSmaller {     int greater, smaller; };   typedef struct greaterSmaller Struct;   Struct findGreaterSmaller(int a, int b) {     Struct s;     if (a > b) {         s.greater = a;         s.smaller = b;     }     else {         s.greater = b;         s.smaller = a;     }       return s; }   // Driver code int main() {     int x, y;     Struct result;       printf("Enter two numbers: ");     scanf("%d%d", &x, &y);       // The last two arguments are passed     // by giving addresses of memory locations     result = findGreaterSmaller(x, y);     printf("The greater number is %d and the"           "smaller number is %d",            result.greater, result.smaller);       return 0; } 

输出:

Enter two numbers: 5 8The greater number is 8 and the smaller number is 5

3)使用数组返回多个值(仅当返回的项属于同一类型时有效):当数组作为参数传递时,其基地址将传递给函数,因此对数组副本所做的任何更改都将在原始数组中更改。

以下是使用数组返回多个值的程序,即在arr[0]存储更大的值,在arr[1]存储较小的值。

// Modified program using array #include    // Store the greater element at 0th index void findGreaterSmaller(int a, int b, int arr[]) {       // Store the greater element at     // 0th index of the array     if (a > b) {         arr[0] = a;         arr[1] = b;     }     else {         arr[0] = b;         arr[1] = a;     } }   // Driver code int main() {     int x, y;     int arr[2];       printf("Enter two numbers: ");     scanf("%d%d", &x, &y);       findGreaterSmaller(x, y, arr);       printf("The greater number is %d and the"           "smaller number is %d",            arr[0], arr[1]);       return 0; } 

输出:

Enter two numbers: 5 8The greater number is 8 and the smaller number is 5

仅C++方法

1)使用引用返回多个值:我们在C++中使用引用来存储返回的值。

// Modified program using References in C++ #include    void compare(int a, int b, int &add_great, int &add_small) {     if (a > b) {         add_great = a;         add_small = b;     }     else {         add_great = b;         add_small = a;     } }   // Driver code int main() {     int great, small, x, y;       printf("Enter two numbers: ");     scanf("%d%d", &x, &y);       // The last two arguments are passed     // by giving addresses of memory locations     compare(x, y, great, small);     printf("The greater number is %d and the"           "smaller number is %d",            great, small);       return 0; } 

输出:

Enter two numbers: 5 8The greater number is 8 and the smaller number is 5

2)使用Class和Object返回多个值:这个想法类似于结构。我们使用两个整数变量创建一个类,并将较大和较小的值存储到这些变量中,然后使用该结构的值。

// Modified program using class #include    class GreaterSmaller { public:     int greater, smaller; };   GreaterSmaller findGreaterSmaller(int a, int b) {     GreaterSmaller s;     if (a > b) {         s.greater = a;         s.smaller = b;     }     else {         s.greater = b;         s.smaller = a;     }       return s; }   // Driver code int main() {     int x, y;     GreaterSmaller result;       printf("Enter two numbers: ");     scanf("%d%d", &x, &y);       // The last two arguments are passed     // by giving addresses of memory locations     result = findGreaterSmaller(x, y);     printf("The greater number is %d and the"           "smaller number is %d",            result.greater, result.smaller);       return 0; } 

输出:

Enter two numbers: 5 8The greater number is 8 and the smaller number is 5

3)使用STL元组返回多个值:这个想法类似于结构。我们用两个整数变量创建一个元组并返回该元组,然后在main函数内部,我们使用tie fucntion将值分配给函数返回的min和max。

// Modified program using C++ STL tuple #include #include   using namespace std;   tuple  findGreaterSmaller(int a, int b) {     if (a < b) {     return make_tuple(a, b);     }     else {     return make_tuple(b, a);     } }   // Driver code int main() {     int x = 5, y= 8;     int max, min;     tie(min, max) = findGreaterSmaller(x, y);       printf("The greater number is %d and the "        "smaller number is %d",         max, min);       return 0; } 

输出:

The greater number is 8 and the smaller number is 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值