c++中函数放在等号右边_如何从C或C++中的函数返回多个值?
新程序员通常在寻找从函数返回多个值的方法。不幸的是,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
c++中函数放在等号右边_如何从C或C++中的函数返回多个值?相关推荐
- 廖雪峰讲python高阶函数求导公式_一文读懂Python 高阶函数
高阶函数 将函数作为参数传入,这样的函数称为高阶函数.函数式编程就是指这种高度抽象的编程范式. 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函 ...
- codeblocks printf函数打印不出来_最全C语言基本程序交互函数之输出到屏幕
前言 上一栏目主要讲解了各种数据类型的知识,大家先闭眼回顾一下数据类型的知识哦.本章节主要内容是讲解程序的基本交互设计之程序的输出.程序和人交互无非就是通过外设进行输入信息,C语言中基本的交互的基本流 ...
- python中sqrt(4)*sqrt(9)_【单选题】Python表达式sqrt(4)*sqrt(9)的值为
[单选题]Python表达式sqrt(4)*sqrt(9)的值为 更多相关问题 构成营业利润的要素主要包括(). A.营业收入 B.营业成本 C.营业税金及附加 D.所得税费用 E.管理费用 已知二次 ...
- 关于mysql叙述中错误的是什么_以下关于MySQL的叙述中,错误的是(1.0分)_学小易找答案...
[单选题]学生表tb_student包含学号sno.学生姓名sname.性别sex.年龄age.所在院系dept.籍贯native等字段,其中age为整型,其余字段均为字符型.现插入一条计算机学院学生 ...
- python中row是什么意思_一文搞懂Python中的yield
关注公众号「Python七号」,及时 get Python 技能. yield 可以实现生成器,可以实现协程. 什么是生成器,什么是协程,如果还不了解,可以继续往下看,概念可以不懂,只要理解它的作用和 ...
- java中i+=2什么意思_三分钟看懂Java中i++与++i的性能差别以及循环中如何使用
在Java中,自增是一种非常常见的操作,在自增中,有两种写法,一种是前缀自增(++i),一种是后缀自增(i++).这里主要简单介绍两种自增的差别. 一.含义差别 前缀自增和后缀自增是不同的.前缀自增( ...
- java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?...
java中为按钮添加图片 No, it is not possible to define private and protected modifiers for the members in int ...
- 心理学在生活中的表现和应用_心理学在我们日常生活中的应用
心理学在我们日常生活中的应用 2015-12-09 14:55公开日志已被浏览4821次 心理学看起来很神秘,离我们很遥远,但实际上在我们生活中无处不在.比如"从众效应",&quo ...
- 实现文件中名词的统计计数_通过勤哲EXCEL和Excel中的rank函数实现排名统计
如今,信息化已成为各行业企业转型和发展的关键所在,信息化技术最明显的特点是企业不同部门的人在信息技术的支撑下,可以利用丰富的资源与工具展开协作学习,在相对自由的模式和环境下,改变传统设计相对局限.固定 ...
最新文章
- 《Ext JS权威指南》印出来了,大家很快就能拿到书了
- 用计算机怎么开5次方,用科学计算器来求三的五次方的值,按键顺序是( )?
- Spring @Configuration和FactoryBean
- 10-排序6 Sort with Swap(0, i) (25 分)
- 9206-初识html
- overridePendingTransition介绍
- java里当显式请求注释时才接受类名称
- 2021-05-31驱动总裁万能网卡版
- python爬虫之喜马拉雅非vip音频下载
- “年少当攀第一流,恰如明月冠中秋”
- GCode_interpreter解读
- 毕业论文参考文献格式GB/T 7714的Endnote设置教程
- 尽管凭借主持人的身份成名,张绍刚先生在内心深处却对这一角色认可度很低
- 《国史通鉴》- 宋朝
- 2022-2028全球阻隔吹膜生产线行业调研及趋势分析报告
- OAS、Swagger和Springfox
- php 容器源码分析,Pimple运行流程浅析(PHP容器)
- 马丁富勒微服务论文连接
- EMI共模电感一般什么材质你知道吗
- 【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
热门文章
- hdu 4739 状压DP
- DirectX 3D相关资源参考
- 山海演武传·黄道·第一卷 雏龙惊蛰 第二十二 ~ 二十四章 真龙之剑·星墟列将...
- STM32_DMA 标准初始化设置解释
- android自带的nsd发现服务器,Android网络服务发现(NSD)协议的使用
- python正则表达式——re模块
- c++新特性11 (10)shared_ptr七reset
- (chap5 web服务器) 保存资源的缓存
- DES对称加密(2)三重DES
- 近世代数--有限交换群--存在子群的阶是群阶的因子