从 C 或 C++ 中的函数返回多个数据项
我对几个家庭作业问题感到困惑...... 可以使用 return() 从函数返回多个数据项吗?一个函数只能返回一个值,除非它是一个指向数组的指针吗?
我相信答案是一个函数可以通过返回一个结构体来返回多个数据项。那么,返回指向数组的指针并不是唯一的方法 - 如果这是一种方法?
但是关于这个主题似乎有很多讨论,所以我想确保我至少有正确的基本想法:您可以使用结构返回多个数据项,但使用指针(我不明白这一点)将使用记忆更有效。这是正确的吗?
I am confused on a couple homework questions I have...
Can you return multiple data items from a function by using return()? Can a function only return one value, unless it is a pointer to an array?
I believe that the answer is that a function can return multiple data items by returning a structure. Then, returning a pointer to an array is not the only way - if that is a way?
But there seems to be a lot of discussion on this topic and so I want to make sure I have at least the basic idea correct: You can return multiple data items using a structure but using pointer (I don't understand this) will use memory more efficiently. Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 C++0x/C++11,你可以使用这个:
编译它
并且如果你运行该程序,它将输出这个:
但最好使用这样的引用(我会说):
这就是我得到的没仔细看问题...
是的,您只能返回 1 个单个值,但该单个值可以包含乘法值(结构、类、数组)。
真的。但是当你使用指针时,这取决于你如何使用它。
当您动态分配它的每个函数调用时,它不会非常有效,并且您需要在使用后手动释放内存。当您使用全局数组/结构时,它会很有效。但是当您多次调用该函数时可能会给您带来问题。
With C++0x/C++11 you can use this:
Compile it with
And and if you run the programm it will output this:
But it's better to use references like this (i would say):
Uch thats what I get for not reading the question carefully...
Yeah you only can return 1 single value, but this single value can include multiply values (struct, class, array).
True. But when you use pointers it depends on how you use it.
When you dynamic allocate it each function call it wont be very efficient and you would need to deallocate the memory manually after usage. When you use a global-array/struct it will be efficient. But can give you problems when you call the function multiply times.
除了本线程中已经说过的内容之外,在 C++11 中,您还可以返回使用 统一初始化:
我个人更喜欢返回具有命名成员的结构而不是元组,因为后者不为其成员提供名称。
In addition to what is already said in this thread, in C++11 you can return structures initialized using uniform initialization:
Personally I prefer returning structures with named members rather than tuples because the latter doesn't provide names for its members.
这是正确的。
但是,您可以通过添加通过引用传递的参数,然后将多个结果写入其中来“返回”多个项目。
That is correct.
You can however "return" multiple items, by adding parameters that are passed by reference, then writing the multiple results to them.
一个函数确实只能通过它的 return 语句返回一个“东西”。然而,这个东西可以是一个指针(C 和 C++ 数组只是伪装的指针)、一个引用(一个不能重新定位或数组索引的指针)或一个对象,它可以封装多个东西。
如果返回一个结构,则将传回整个结构。如果返回指针或引用,则仅返回结构的地址 - 因此最好不要返回指向函数返回时超出范围的结构的引用或指针!这样做会调用未定义的行为,这很可能(但并非总是)是分段错误。
A function can indeed only return one 'thing' with its return statement. That thing can, however, be a pointer (C & C++ arrays are simply pointers in disguise), a reference (a pointer which can't be reseated or array-indexed) or an object, which may encapsulate multiple things.
If you return a structure, you're passing back the entire structure. If you return a pointer or reference, you are returning only the address of the structure - so you had better not return a reference or pointer to a structure that goes out of scope when the function returns! Doing so invokes undefined behavior, which most likely (but not always) is a segmentation fault.
如果您想了解更多有关按值传递参数和按引用传递参数的信息,它也适用于返回参数。
正如您提到的:
假设您有一些结构:
和一些方法:
此方法说明了将参数传递给方法并按值返回它的情况,这意味着每次调用此方法时,都会将该方法的参数复制到内存中的另一个位置。这种复制需要时间,当你有更大的结构时,它会减慢你的程序运行时间,因此通过引用传递大结构会更有效。
在这种情况下,您不会复制整个数据结构,而只是传递该结构所在的内存地址。您也不需要 return 语句,因为对该结构对象的更改将在此方法之外可见。但是,如果您将 bigStruct 重新分配给新的内存位置,它将仅在本地方法中可见:
workWithReference
我希望现在更清楚了;)
If you want a bigger picture about this read about passing parameters by value and passing by reference it also applies for returning parameters.
As you mentioned:
Lets say you have some structure:
and some method:
This method illustrates the case when you pass parameters to the method and return it by value, which means that each time you call this method you are copying the method's argument into another place in memory. This copying takes time and when you have bigger structures it slows down your program run time thus it is more efficient to pass big structures by reference.
In this case you are not copying whole data structure, just passing the address of the memory where this structure resides. You also don't need a return statement since changes to that structure object will be visible outside this method. But if you will reassign bigStruct to a new memory location it will be visible only within local method:
workWithReference
I hope it's more clearer now;)