微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C不能从map调用lambda表达式

我已经创建了一个温度转换类,我决定创建一个具有如下结构的转换表:
temperature | conversion formula

出于这个原因,我创建了一个std :: map,其中键作为温度指数,然后是公式.在这里你可以看到实现:

enum class TempType {Celsius,Fahrenheit,Kelvin,Rankine,Delisle,Newton,Reaumur,Romer};

class Temperature {
private:

  double value;   //value of the temperature
  TempType kind;  //Celsius,Fahrenheit...

  //conversion tables
  std::map<TempType,std::function<double(double)>> fromCelsius = {
      { TempType::Fahrenheit,[](double x) { return x * (9/5) + 32; } },{ TempType::Kelvin,[](double x) { return x + 273.15; } },{ TempType::Rankine,[](double x) { return (x + 273.15) * (9/5); } },{ TempType::Delisle,[](double x) { return (100 - x) * (3/2); } },{ TempType::Reaumur,[](double x) { return x * (33/100); } },{ TempType::Newton,[](double x) { return x * (4/5); } },{ TempType::Romer,[](double x) { return x * (21/40) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromFahrenheit = {
      { TempType::Celsius,[](double x) { return (x - 32) * (5/9); } },[](double x) { return (x + 459.67) * (5/9); } },[](double x) { return x + 459.67; } },[](double x) { return (212 - x) * (5/6); } },[](double x) { return (x - 32) * (11/60); } },[](double x) { return (x - 32) * (4/9); } },[](double x) { return (x - 32) * (7/24) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromKelvin = {
      { TempType::Celsius,[](double x) { return x - 273.15; } },{ TempType::Fahrenheit,[](double x) { return x * (9/5) - 459.67; } },[](double x) { return x * (9/5); } },[](double x) { return (373.15 - x) * (3/2); } },[](double x) { return (x - 273.15) * (33/100); } },[](double x) { return (x - 273.15) * (4/5); } },[](double x) { return (x - 273.15) * (21/40) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromrankine = {
      { TempType::Celsius,[](double x) { return (x - 491.67) * (5/9); } },[](double x) { return x - 459.67; } },[](double x) { return x * (5/9); } },[](double x) { return (671.67 - x) * (5/6); } },[](double x) { return (x - 491.67) * (11/60); } },[](double x) { return (x - 491.67) * (4/9); } },[](double x) { return (x - 491.67) * (7/24) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromDelisle = {
      { TempType::Celsius,[](double x) { return 100 - x * (5/9); } },[](double x) { return 212 - x * (6/5); } },[](double x) { return 373.15 - x * (2/3); } },[](double x) { return 671.67 - x * (6/5); } },[](double x) { return 33 - x * (11/50); } },[](double x) { return 80 - x * (8/15); } },[](double x) { return 60 - x * (7/20); } }
  };

  std::map<TempType,std::function<double(double)>> fromNewton = {
      { TempType::Celsius,[](double x) { return x * (100/33); } },[](double x) { return x * (60/11) + 32; } },[](double x) { return x * (100/33) + 273.15; } },[](double x) { return x * (60/11) + 491.67; } },[](double x) { return (33 - x) * (50/11); } },[](double x) { return x * (80/33); } },[](double x) { return x * (35/22) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromreamur = {
      { TempType::Celsius,[](double x) { return x * (5/4); } },[](double x) { return x * (9/4) + 32; } },[](double x) { return x * (5/4) + 273.15; } },[](double x) { return x * (9/4) + 491.67; } },[](double x) { return (80 - x) * (15/8); } },[](double x) { return x * (33/80); } },[](double x) { return x * (21/32) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromromer = {
      { TempType::Celsius,[](double x) { return (x - 7.5) * (40/21); } },[](double x) { return (x - 7.5) * (24/7) + 32; } },[](double x) { return (x - 7.5) * (40/21) + 273.15; } },[](double x) { return (x - 7.5) * (24/7) + 491.67; } },[](double x) { return (60 - x) * (20/7); } },[](double x) { return (x - 7.5) * (22/35); } },[](double x) { return (x - 7.5) * (32/21); } }
  };

public:
}    
#endif // TEMPERATURE_H

现在,如果我想从温度对象转换为另一个,我使用此代码

Temperature x(20,TempType::Celsius); //20 °C
double s = x.convertTo(TempType::Fahrenheit); //convert 20 °c to 68 °F

转换代码是这样的:

double Temperature::convertTo(const TempType& temperature) const {

  if (temperature == kind) {
    return value;
  }

  double result = -1;

  switch (temperature) {
    case TempType::Celsius:
      result = fromCelsius.at(kind)(value);
      break;
    case TempType::Fahrenheit:
      result = fromFahrenheit.at(kind)(value);
      break;
    case TempType::Kelvin:
      result = fromKelvin.at(kind)(value);
      break;
    case TempType::Rankine:
      result = fromrankine.at(kind)(value);
      break;
    case TempType::Delisle:
      result = fromDelisle.at(kind)(value);
      break;
    case TempType::Newton:
      result = fromNewton.at(kind)(value);
      break;
    case TempType::Reaumur:
      result = fromreamur.at(kind)(value);
      break;
    case TempType::Romer:
      result = fromromer.at(kind)(value);
      break;
    default:
      break;
    }

  return result;

}

我使用地图的键(TempType),然后我将参数传递给lambda.返回的结果总是0.任何想法?

我正在使用QTCreator和mingw.

解决方法

在C 4/5中,21 / 40,5 / 6等都是零,因为除法是以整数运算执行的.

你需要写4./5.

原文地址:https://www.jb51.cc/c/111345.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐