【发布时间】:2019-01-02 18:48:33
【问题描述】:
我需要返回两个双打。我对按引用传递有点了解,但我不确定如何使用它来返回多个。这是我到目前为止所尝试的。我做了两个参考参数并将其添加到我希望返回两个值的方法中。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
double min;
double max;
getDistance(766, 981, 328, 609, min, max);
getDistance(899, 171, 1009, 282, min, max);
cout << "Minimum distance is "+minDistance+"inches";
cout << "Maximum distance is "+maxDistance+"inches";
return 0;
}
void getDistance(int left, int right, int top, int bottom, double& minDistance, double& maxDistance) {
int height=bottom - top;
int width=right - left;
bool isCNN=width !=height;
if (isCNN) {
if (width >=215 && height >=281) minDistance=0;
maxDistance=14;
else if (width >=124 && height >=167) minDistance=14;
maxDistance=33.75;
else if (width >=76 && height >=111) minDistance=33.75;
maxDistance=53.5;
else if (width >=56 && height >=94) minDistance=53.5;
maxDistance=73.25;
else if (width >=49 && height >=84) minDistance=73.25;
maxDistance=93;
else if (width >=41 && height >=71) minDistance=93;
maxDistance=112.75;
else if (width >=28 && height >=57) minDistance=132.5;
maxDistance=172;
else if (width >=23 && height >=49) minDistance=191.75;
maxDistance=270.75;
else minDistance=270.75;
maxDistance=480;
}
else {
if (width >=330) minDistance=0;
maxDistance=6.375;
else if (width >=238) minDistance=6.375;
maxDistance=16.25;
else if (width >=168) minDistance=16.25;
maxDistance=26.125;
else if (width >=122) minDistance=26.125;
maxDistance=36;
else if (width >=108) minDistance=36;
maxDistance=55.75;
else if (width >=91) minDistance=55.75;
maxDistance=75.5;
else minDistance=75.5;
maxDistance=144;
}
return;
}
价值观。
【问题讨论】:
-
只需传递两个引用。每个返回值一个。
-
你应该展示你迄今为止所尝试的。如果你不知道如何通过引用,我建议从good c++ book学习。
-
请不要这样做。返回两个值。
-
if和else语句的使用有点不靠谱。if (width >=215 && height >=281) minDistance=0;是整个if。maxDistance=14;在if之外,因此else断开连接。这会导致一连串错误消息。大括号是必需的:if (width >= 215 && height >= 281) { minDistance = 0; maxDistance = 14; }
标签: c++ xcode pass-by-reference multiple-return-values