C++ 函数可以返回多个值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6394741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:01:56  来源:igfitidea点击:

Can a C++ function return more than one value?

c++

提问by Sudhir Chopra

Possible Duplicate:
can a function return more than one value?

可能重复:
一个函数可以返回多个值吗?

I want to return 3 variables from a c++ function but as one function can have only one running return value how I can return 3 values. tried using return(5,4); but this is an syntax error.

我想从 C++ 函数返回 3 个变量,但是作为一个函数只能有一个运行返回值,我如何返回 3 个值。尝试使用 return(5,4); 但这是一个语法错误。

回答by Sven

A C++ function can return only one value. However, you can return multiple values by wrapping them in a class or struct.

C++ 函数只能返回一个值。但是,您可以通过将它们包装在一个类或结构中来返回多个值。

struct Foo
{
     int value1;
     int value2;
};

Foo SomeFunction()
{
    Foo result = { 5, 4 };
    return result;
}

Or you could use std::tuple, if that is available with your compiler.

或者您可以使用std::tuple,如果您的编译器可用。

#include <tuple>

std::tuple<int, int, int> SomeFunction()
{
    return std::make_tuple(5, 4, 3);
}

If you don't know in advance how many values you're going to return, a std::vectoror a similar container is your best bet.

如果您事先不知道要返回多少个值,那么std::vector最好选择一个或类似的容器。

You can also return multiple values by having pointer or reference arguments to your function and modifying them in your function, but I think returning a compound type is generally speaking a "cleaner" approach.

您还可以通过将指针或引用参数指向您的函数并在您的函数中修改它们来返回多个值,但我认为返回复合类型通常是一种“更干净”的方法。

回答by Mat

You can only return one value in C++. If you need to return more information, return a structure (or a container like a std::vectorfor example), or pass in some variables as non-const references (or pointers) and change those.

在 C++ 中只能返回一个值。如果您需要返回更多信息,请返回一个结构(或std::vector例如a 之类的容器),或者将一些变量作为非常量引用(或指针)传入并更改它们。

回答by Nawaz

tried using return(5,4); but this is an syntax error.

尝试使用 return(5,4); 但这是一个语法错误。

That is not a syntax error. (5,4)is a valid expression and here ,is an operator. So the expression (5,4)evaluates to the rightmost operand, which is 4. Hence it will return 4.

这不是语法错误。(5,4)是一个有效的表达式,这里,是一个运算符。因此表达式(5,4)计算为最右边的操作数,即4。因此它将返回4



Now coming to your problem: define a structif any existing one doesn't help you, and return an object of structinstead, as:

现在来解决您的问题:struct如果现有的任何一个对您没有帮助,请定义一个,并返回一个struct对象,如下所示:

struct values
{
   int i;
   int j;
   char *other;
};

values f()
{
  values v = {/*....*/};
   //...
   return v;
}

And if typeof all values is same, then you can use std::vectoras:

如果所有值的类型都相同,那么您可以使用std::vector

#include <vector>  //must include it!

std::vector<int> f()
{
   std::vector<int> v;
   v.push_back(5);
   v.push_back(4);
   //so on
   //...
   return v;
}

There are other containers as well, viz. std::map, std::list, std::stack, etc. Use which suits you the best. There is also std::pairwhich holds only two values, as the name implies.

还有其他容器,即。std::mapstd::liststd::stack等。使用最适合您的。std::pair顾名思义,还有一个只包含两个值。

回答by sbi

There are several ways to achieve that.

有几种方法可以实现这一目标。

  1. If you have objects of the same type, std::arraywould be a good return type for that.
    If you have varying numbers, use std::vectorinstead.

  2. If you have (a fixed number of) different types, use std::tuple:

    std::tuple <int, long, double> f()
    {
      return std::tuple <int, long, double>( 42, 4711l, 3.141 );
    }
    
  1. 如果您有相同类型的对象,那std::array将是一个很好的返回类型。
    如果您有不同的数字,请std::vector改用。

  2. 如果您有(固定数量的)不同类型,请使用std::tuple

    std::tuple <int, long, double> f()
    {
      return std::tuple <int, long, double>( 42, 4711l, 3.141 );
    }
    

I know no way to return a varying number of non-polymorphic objects of different types built into the language or standard library.

我不知道如何返回语言或标准库中内置的不同类型的不同数量的非多态对象。

回答by luvieere

Or you could use std::pairfor two results and look at boost::tuplefor more than two. It can even handle different types in the same tuple.

或者您可以使用std::pair两个结果并查看两个以上的结果boost::tuple。它甚至可以处理同一个元组中的不同类型。

回答by SteeveDroz

No, but there are two ways to do that:

不,但有两种方法可以做到这一点:

1) Give the pointers to the values to "return" as parameters, you change the value of it in the function.

1)将指向值的指针作为参数“返回”,您可以在函数中更改它的值。

2) You may create a structthat contains all these values and return the struct. Use this only if it is really necessary, though, the fist solution is much better.

2)您可以创建一个struct包含所有这些值并返回结构体。仅在确实有必要时才使用它,不过,第一个解决方案要好得多。

回答by Jasmijn

You can't. You can, however, do tricks to fake it. For example, make your function return a pointer to int, and returning the pointer to an array of ints.

你不能。但是,您可以使用技巧来伪造它。例如,让您的函数返回一个指向 int 的指针,并返回指向一个 int 数组的指针。

回答by phoxis

return (a, b, c, ... , n)The statements a, b, care separated with comma operators and the expression evaluates to the rightmost operand n. So the syntax is correct, but does not return multiple values.

return (a, b, c, ... , n)的陈述abc用逗号运营商和该表达式的最右边的操作数分开n。所以语法是正确的,但不返回多个值。

To return multiple values:

返回多个值:

  • Put the variables you want to return in a structure and return it.
  • Or make an array of multiple variables (dynamically allocated) and return the base address
  • 将要返回的变量放入一个结构体中并返回。
  • 或者制作一个包含多个变量的数组(动态分配)并返回基地址

回答by Priyank Bhatnagar

Most of the answers here are correct that you should return a struct. There is one more way if you are interested, and that is by sending reference of the variables that you want to return.

这里的大多数答案都是正确的,您应该返回一个结构。如果您感兴趣,还有另一种方法,那就是发送您想要返回的变量的引用。

For example:

例如:

#include<iostream>
#include<cstdio>
using namespace std;
bool f(int a,int b,int& q,int& r)
{
    if(b==0)return 0;
    q=a/b;
    r=a%b;
    return 1;
}

int main()
{
    int a=64,b=7,q,r;
    bool pos=f(a,b,q,r);
    if(pos)
    {
        printf("Quotient = %d Remainder = %d\n",q,r);
    }
    else
    {
        printf("Divison by zero not possible!\n");
    }
    return 0;
}

回答by Vivek Khanna

It is possible to return more than one values from a function you have to use pointer for that case. Also this is possible by using the pointer arguments pointer arguments keep their value' Detailed usage of pointer for returning more than one value is at http://www.techyv.com/questions/how-do-i-return-multiple-variables-c-function

可以从一个函数返回多个值,您必须在这种情况下使用指针。这也可以通过使用指针参数指针参数保持它们的值'返回多个值的指针的详细用法位于http://www.techyv.com/questions/how-do-i-return-multiple-variables -c-函数