回答
可以传递给两个双打引用到一个函数,设置它们的值在函数内部
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