回答
可以傳遞給兩個雙打引用到一個函數,設置它們的值在函數內部
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
您不能從一個函數返回兩個值,但可以返回一個指向包含兩個雙精度值的數組或其他結構的指針。
你可以寫一個簡單的結構保存變量並返回它,或者使用std::pair
或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
}
也可能想提及['std :: tie'](http://en.cppreference.com/w/cpp/utility/tuple/tie),它可以用來在兩個單獨的,現有變量。 – 2013-03-12 16:04:31
@ BenjaminLindley很好的建議。增加了一個例子。 – juanchopanza 2013-03-12 16:13:15
從技術上講,沒有你不能在路上返回兩個變量,你通常會返回一個變量。但是,您可以使用參考。這樣的話,你可以傳遞多個變量的函數,該函數將它們分配,而不是返回什麼:
void function(double & param1, double & param2) {
param1 = 6.28;
param2 = 3.14;
}
而你也這樣稱呼它:
double var1, var2;
function(var1, var2);
你不能直接做(因爲返回值是單數)。
但是,您可以在結構中放入幾個值,然後返回(如一對<>)。
一個常見的模式是參考返回輸出變量:
ReturnVal Myfunction(/*in*/ BlahType _someParameters, /*out*/ ReturnType& _firstReturn, /*out*/ OtherReturnType& _secondReturn)
{
_firstReturn = //someStuff
_secondReturn = //someOtherStuff
return SUCCESS;
}
不,你不能返回兩個變量,你需要通過參考方法
#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調用交換函數之後
這是返回兩個值
如果您使用C++ 11,我會說,理想的方式是使用std::tuple
和std::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';
}
- 1. 從函數返回兩個變量
- 2. C函數返回兩個值
- 3. 從函數返回變量
- 4. 函數不返回變量
- 5. 變量返回函數
- 6. 從函數返回一個變量
- 7. 從JavaScript函數返回多個變量?
- 8. C++ - 函數返回向量
- 9. 函數返回函數的變量 - Python
- 10. C++ Lambdas:函數返回一個函數,返回一個函數...?
- 11. 函數返回兩個值
- 12. 在JS/JSON函數內返回變量
- 13. 在函數外返回變量
- 14. 返回函數中的變量
- 15. 在c#函數中返回一個值
- 16. 從標量值函數返回變量
- 17. 使用c函數返回多個變量
- 18. 如何在JavaScript函數中設置和返回一個變量?
- 19. 在Matlab中保存函數返回多個變量的返回值
- 20. 劃分兩個變量返回0
- 21. 傳遞一個數組變量函數返回一個數組變量
- 22. 使用靜態變量返回數組中的下一個值的C++函數
- 23. R中的函數可以返回兩個向量嗎?
- 24. python中兩個變量的表函數
- 25. 如何從另一個函數訪問一個變量而不在原始函數中返回該變量?
- 26. C++ ReSharper - 生成函數返回值/參數類型的變量
- 27. 返回指向本地函數變量
- 28. 函數返回未分配給變量
- 29. 從函數返回'undefined'變量
- 30. 從函數reset返回的變量
見http://stackoverflow.com/q/321068/10077 – 2013-03-12 15:55:11