C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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.

大佬总结

以上是大佬教程为你收集整理的C不能从map调用lambda表达式全部内容,希望文章能够帮你解决C不能从map调用lambda表达式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。