在 C++ 函数中返回两个变量

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

Returning two variables in a C++ function

c++

提问by Zeus M

I would like to return two double variables: when calling a function that I have created. According to some tutorials (which deal with the basics of the C++), I would be unable to do that.

我想返回两个双变量:调用我创建的函数时。根据一些教程(涉及 C++ 的基础知识),我将无法做到这一点。

Is there a way to do so?

有没有办法这样做?

回答by juanchopanza

You could write a simple struct that holds the variables and return it, or use an std::pairor std::tuple:

您可以编写一个简单的结构来保存变量并返回它,或者使用std::pairor std::tuple

#include <utility>

std::pair<double, double> foo()
{
  return std::make_pair(42., 3.14);
}

#include <iostream>
#include <tuple> // C++11, for std::tie
int main()
{
  std::pair<double, double> p = foo();
  std::cout << p.first << ", " << p.second << std::endl;

  // C++11: use std::tie to unpack into pre-existing variables
  double x, y;
  std::tie(x,y) = foo();
  std::cout << x << ", " << y << std::endl;

  // C++17: structured bindings
  auto [xx, yy] = foo(); // xx, yy are double
}

回答by simonc

You can pass references to two doubles into a function, setting their values inside the function

您可以将两个双精度的引用传递给一个函数,在函数内设置它们的值

void setTwoDoubles(double& d1, double& d2)
{
    d1 = 1.0;
    d2 = 2.0;
}

double d1, d2;
setTwoDoubles(d1, d2);
std::cout << "d1=" << d1 << ", d2=" << d2 << std::endl

回答by Timothy Shields

If you're using C++11, I'd say the ideal way is to use std::tupleand std::tie.

如果您使用的是 C++11,我会说理想的方法是使用std::tupleand std::tie

Example taken from the std::tuplepage I linked to:

std::tuple我链接到的页面中获取的示例:

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

std::tuple<double, char, std::string> get_student(int id)
{
    if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson");
    if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
    if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum");
    throw std::invalid_argument("id");
}

int main()
{
    auto student0 = get_student(0);
    std::cout << "ID: 0, "
              << "GPA: " << std::get<0>(student0) << ", "
              << "grade: " << std::get<1>(student0) << ", "
              << "name: " << std::get<2>(student0) << '\n';

    double gpa1;
    char grade1;
    std::string name1;
    std::tie(gpa1, grade1, name1) = get_student(1);
    std::cout << "ID: 1, "
              << "GPA: " << gpa1 << ", "
              << "grade: " << grade1 << ", "
              << "name: " << name1 << '\n';
}

回答by antonijn

Technically, no you are not able to return two variables in the way you would normally return a variable. You can, however, use references. That way, you can pass multiple variables to a function, and the function will assign them, rather than returning anything:

从技术上讲,不,您不能以通常返回变量的方式返回两个变量。但是,您可以使用引用。这样,您可以将多个变量传递给一个函数,该函数将分配它们,而不是返回任何内容:

void function(double & param1, double & param2) {
    param1 = 6.28;
    param2 = 3.14;
}

And you would call it like this:

你会这样称呼它:

double var1, var2;
function(var1, var2);

回答by unwind

You can use a std::pair, for instance.

std::pair例如,您可以使用 a 。

回答by Maadh

No you can not return two variables you need to use by reference method as

不,您不能通过引用方法返回需要使用的两个变量

    #include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);

int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}


// function definition to swap the values.
void swap(int &x, int &y)
{
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

the output will be 100 // x before calling swap function 200 // y before calling swap function 200 // x after calling swap function 100 // y after calling swap function

输出将是 100 // 调用交换函数之前的 x 200 // 调用交换函数之前的 y 200 // 调用交换函数之后的 x 100 // 调用交换函数之后的 y

this as return two values

这作为返回两个值

this link help you

这个链接可以帮助你

回答by Yochai Timmer

You can't do it directly (because a return value is singular).
But, you could put a few values in a structure, and return that (like a pair<>).

您不能直接执行此操作(因为返回值是单数)。
但是,您可以将一些值放入一个结构中,然后返回它(如 pair<>)。

A common pattern is to return output variables by reference:

一个常见的模式是通过引用返回输出变量:

ReturnVal Myfunction(/*in*/ BlahType _someParameters, /*out*/ ReturnType& _firstReturn, /*out*/ OtherReturnType& _secondReturn)
{
   _firstReturn = //someStuff
   _secondReturn = //someOtherStuff


return SUCCESS;
}

回答by RouteMapper

You cannot return two values from one function, but you could return a pointer to an array or some other structure which contains the two doubles.

你不能从一个函数返回两个值,但你可以返回一个指向数组或包含两个双精度数的其他结构的指针。