从 C 或 C++ 中的函数返回多个数据项 [英] Returning multiple data items from a function in C or C++

查看:77
本文介绍了从 C 或 C++ 中的函数返回多个数据项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的几个家庭作业问题感到困惑...可以使用 return() 从函数返回多个数据项吗?一个函数能否只返回一个值,除非它是一个指向数组的指针?

我相信答案是一个函数可以通过返回一个结构来返回多个数据项.那么,返回一个指向数组的指针并不是唯一的方法——如果这是一种方法?

但似乎有很多关于这个主题的讨论,所以我想确保我至少有一个正确的基本思想:你可以使用结构返回多个数据项,但使用指针(我不明白这个) 将更有效地使用内存.这是正确的吗?

解决方案

在 C++0x/C++11 中你可以使用这个:

#include #include #include <元组>使用命名空间标准;元组<整数,无符号,字符串,整数>获取值(){返回元组(1, 2, "3", 4);}int main(){一个;无符号 b;字符串 c;国际d;领带(a, b, c, d) = getValues();cout<<<<", " <<<<", " <<c<<", " <<d<<结束;返回0;}

编译它

g++ tuple_test.cpp -std=c++0x -o tuple_test

如果你运行程序,它会输出:

1, 2, 3, 4

但最好使用这样的引用(我会说):

void getValues(int& a, unsigned& b, string& c, int& d){一 = 1;b = 2;c = "3";d = 4;}int main(){...getValues(a, b, c, d)...}

这就是我没有仔细阅读问题的结果......

<块引用><块引用>

一个函数能否只返回一个值,除非它是一个指向数组的指针?

是的,您只能返回 1 个单值,但该单值可以包含乘法值(结构、类、数组).

<块引用><块引用>

您可以使用结构返回多个数据项,但使用指针(我不明白这一点)会更有效地使用内存.这是正确的吗?

没错.但是当你使用指针时,它取决于你如何使用它.当您动态分配每个函数调用时,它的效率不会很高,您需要在使用后手动释放内存.当您使用全局数组/结构时,它将是高效的.但是当你多次调用函数时会给你带来问题.

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?

解决方案

With C++0x/C++11 you can use this:

#include <string>
#include <iostream>
#include <tuple>

using namespace std;

tuple<int, unsigned, string, int> getValues()
{
    return tuple<int, unsigned, string, int>(1, 2, "3", 4);
}

int main()
{
    int a;
    unsigned b;
    string c;
    int d;

    tie(a, b, c, d) = getValues();

    cout << a << ", " << b << ", " << c << ", " << d << endl;
    return 0;
}

Compile it with

g++ tuple_test.cpp -std=c++0x -o tuple_test

And and if you run the programm it will output this:

1, 2, 3, 4

But it's better to use references like this (i would say):

void getValues(int& a, unsigned& b, string& c, int& d)
{
    a = 1;
    b = 2;
    c = "3";
    d = 4;
}

int main()
{
    ...
    getValues(a, b, c, d)
    ...
}

Uch thats what I get for not reading the question carefully...

Can a function only return one value, unless it is a pointer to an array?

Yeah you only can return 1 single value, but this single value can include multiply values (struct, class, array).

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?

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 或 C++ 中的函数返回多个数据项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆