2013-03-12 102 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'; 
}