2013-03-12 107 views
9

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

有没有办法做到这一点?

+1

见http://stackoverflow.com/q/321068/10077 – 2013-03-12 15:55:11

回答

16

可以传递给两个双打引用到一个函数,设置它们的值在函数内部

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 
6

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

+3

然后在C++ 11,你可以用'tie'分配直接到两个变量。 – 2013-03-12 16:06:55

0

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

19

你可以写一个简单的结构保存变量并返回它,或者使用std::pairstd::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 
} 
+1

也可能想提及['std :: tie'](http://en.cppreference.com/w/cpp/utility/tuple/tie),它可以用来在两个单独的,现有变量。 – 2013-03-12 16:04:31

+0

@ BenjaminLindley很好的建议。增加了一个例子。 – juanchopanza 2013-03-12 16:13:15

3

从技术上讲,没有你不能在路上返回两个变量,你通常会返回一个变量。但是,您可以使用参考。这样的话,你可以传递多个变量的函数,该函数将它们分配,而不是返回什么:

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

而你也这样称呼它:

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

你不能直接做(因为返回值是单数)。
但是,您可以在结构中放入几个值,然后返回(如一对<>)。

一个常见的模式是参考返回输出变量:

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


return SUCCESS; 
} 
3

不,你不能返回两个变量,你需要通过参考方法

#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; 
} 

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

这是返回两个值

this link help you

11

如果您使用C++ 11,我会说,理想的方式是使用std::tuplestd::tie。从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'; 
}