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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2571831/
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-27 23:57:29  来源:igfitidea点击:

can a function return more than one value?

c++language-agnosticreturn-value

提问by Ashish Yadav

Possible Duplicate:
Returning multiple values from a C++ function

可能的重复:
从 C++ 函数返回多个值

can a function return more than one value?

一个函数可以返回多个值吗?

Edit
Except return by reference.

编辑
除了通过引用返回。

采纳答案by rlbond

No, but you can return a pairor boost::tuplewhich can contain multiple values.

不,但您可以返回一个pairor boost::tuple,其中可以包含多个值。

In addition, you can use references to return multiple values like this:

此外,您可以使用引用返回多个值,如下所示:

void MyFunction(int a, int b, int& sum, int& difference);

You would call this function like this:

你可以这样调用这个函数:

int result_sum;
int result_difference;
MyFunction(1, 2, result_sum, result_difference);

As Hogan points out, technically this isn't returningmultiple variables, however it is a good substitute.

正如霍根指出的那样,从技术上讲,这不会返回多个变量,但它是一个很好的替代品。

回答by Josh Townzen

In the boost::tuplelibrary, there's a function called tiethat simplifies the process of getting information out of a returned tuple. If you had a function that returned a tupleof two doubles and wanted to load those into two local variables xand y, you could assign your function's return value to boost::tie(x, y).

boost::tuple库中,有一个被调用的函数tie,它简化了从返回的tuple. 如果您有一个返回tuple两个doubles 中的a 的函数并希望将它们加载到两个局部变量xand 中y,您可以将函数的返回值分配给boost::tie(x, y)

Example:

例子:

#include <math.h>
#include <iostream>
#include <boost/tuple/tuple.hpp>

const double PI = 3.14159265;

boost::tuple<double, double> polar_to_rectangular(double radius, double angle)
{
    return boost::make_tuple(radius * cos(angle), radius * sin(angle));
}

int main()
{
    double x;
    double y;

    boost::tie(x, y) = polar_to_rectangular(4, (45 * PI) / 180);
    std::cout << "x == " << x << ", y == " << y << std::endl;

    return 0;
}

回答by Josh Townzen

Yes - have your function return a struct. Or return the values via reference parameters.

是的 - 让您的函数返回一个结构。或者通过引用参数返回值。

struct A {
   int x, y;
   A(int x, int y) : x(x), y(y) {}
};

A myfun() {
   return A(0, 42);    // return  two values
}

or:

或者:

void myfun(int & a, int & b) {
   a = 0;
   b = 42;
}

回答by Brian R. Bondy

A function can return values in the specified ways:

函数可以以指定的方式返回值:

  • Via return value of any type
  • Via a pointer
  • Via a reference
  • Via setting a global variable (highly not recommended)
  • 通过任何类型的返回值
  • 通过指针
  • 通过参考
  • 通过设置全局变量(强烈不推荐)

If you need a self contained return value, you would typically wrap the types you need in a struct and return an object of that struct by value. If you want to avoid keeping a local copy you would pass in a reference parameter to be modified.

如果您需要一个自包含的返回值,您通常会将您需要的类型包装在一个结构中并按值返回该结构的对象。如果您想避免保留本地副本,您可以传入一个要修改的引用参数。

回答by Saurabh

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
    int a;
    int b;
}Mystruct;

Mystruct myfun(); 

int main()
{
    char name[30];
    Mystruct ms2;
    ms2 = myfun();
    printf("val1: %d   val2: %d",ms2.a,ms2.b);
    return 0;
}

Mystruct myfun()
{
    int a,b;
    Mystruct ms;

    a = 10;
    b = 20;
    ms.a=a;
    ms.b=b;

    return(ms);
}

回答by Saurabh

use structure and return multiple value with different data type.

使用结构并返回具有不同数据类型的多个值。

回答by karthi madheswaran

main()
{
  int a=10,b=20;
  int *c;
  c=aa(a,b);
  printf("%d %d",*c,*c+1);
}

void aa(int a,int b)
{
   int c1[2];
   c1[0]=b+a;
   c1[1]=a-b;
   return(c1);
}

here, the address of c1 will be return. so it will store in main c cariable. we can retrive both variable via pointer,

在这里,将返回 c1 的地址。所以它将存储在 main c cariable 中。我们可以通过指针检索两个变量,