-3

我需要返回两个双打。我对按引用传递有点了解,但我不确定如何使用它来返回多个。这是我到目前为止所尝试的。我做了两个参考参数并将其添加到我希望返回两个值的方法中。

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

价值观。

4

3 回答 3

2

您可以std::pair<double, double>用作返回类型。例如

std::pair<double, double> foo()
{
   return {10, 20};
}
于 2018-07-25T21:05:12.107 回答
0

正如其他人所指出的,除了通过引用传递之外,还有其他方法可以返回 2 个值。如果您确实想通过引用返回,那么您所写的几乎是正确的。而不是在主方法中使用变量“minDistance”和“maxDistance”(它们没有被声明),你应该使用你已经声明的“min”和“max”变量。此外,您应该使用 '<<' 而不是 '+' 在 cout 调用中附加输出。此外,如果您希望输出出现在单独的行上(而不是一起运行),则应以“<< endl”结束该行。最后,正如 user4581301 所说,您的“if”语句的主体不包括 minDistance 和 maxDistance。您需要在语句周围加上大括号才能同时包含这两个语句。

重写代码以考虑这些更改(但不以任何方式确保“计算”的正确性)给出:

#include <iostream>

int main(int, char**) {
  double min;
  double max;
  getDistance(766, 981, 328, 609, min, max);
  getDistance(899, 171, 1009, 282, min, max);
  std::cout << "Minimum distance is " << min << "inches" << std::endl;
  std::cout << "Maximum distance is " << max << "inches" << std::endl;
  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;
}
于 2018-07-26T07:55:39.137 回答
0

如果您真的想通过引用传递“返回”两个值:

void foo(int& x, int& y)
{
    x = 55;
    y = 66;
}

int main()
{
    int x = 0;
    int y = 0;
    foo(x, y);
    if (x == 55 && y == 66)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

这样的事情应该做。正如其他人所指出的那样,实际上还有许多其他方法可以实际返回两个值,但是就在这里。

在您提交的代码中,(除了语法问题),您需要在声明中引用minmax在您的 main 方法中cout。在getDistance()withminDistancemaxDistance中,您已经为传递给它的双打创建了别名,但这些别名在函数之外不存在!

于 2018-07-25T21:35:52.233 回答