gpt4 book ai didi

C++ 无法从映射中调用 lambda 表达式

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:33 25 4
gpt4 key购买 nike

我创建了一个温度转换类,我决定创建一个结构如下的转换表:

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); } },
{ TempType::Kelvin, [](double x) { return (x + 459.67) * (5/9); } },
{ TempType::Rankine, [](double x) { return x + 459.67; } },
{ TempType::Delisle, [](double x) { return (212 - x) * (5/6); } },
{ TempType::Newton, [](double x) { return (x - 32) * (11/60); } },
{ TempType::Reaumur, [](double x) { return (x - 32) * (4/9); } },
{ TempType::Romer, [](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; } },
{ TempType::Rankine, [](double x) { return x * (9/5); } },
{ TempType::Delisle, [](double x) { return (373.15 - x) * (3/2); } },
{ TempType::Newton, [](double x) { return (x - 273.15) * (33/100); } },
{ TempType::Reaumur, [](double x) { return (x - 273.15) * (4/5); } },
{ TempType::Romer, [](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); } },
{ TempType::Fahrenheit, [](double x) { return x - 459.67; } },
{ TempType::Kelvin, [](double x) { return x * (5/9); } },
{ TempType::Delisle, [](double x) { return (671.67 - x) * (5/6); } },
{ TempType::Newton, [](double x) { return (x - 491.67) * (11/60); } },
{ TempType::Reaumur, [](double x) { return (x - 491.67) * (4/9); } },
{ TempType::Romer, [](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); } },
{ TempType::Fahrenheit, [](double x) { return 212 - x * (6/5); } },
{ TempType::Kelvin, [](double x) { return 373.15 - x * (2/3); } },
{ TempType::Rankine, [](double x) { return 671.67 - x * (6/5); } },
{ TempType::Newton, [](double x) { return 33 - x * (11/50); } },
{ TempType::Reaumur, [](double x) { return 80 - x * (8/15); } },
{ TempType::Romer, [](double x) { return 60 - x * (7/20); } }
};

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

std::map<TempType, std::function<double(double)>> fromReamur = {
{ TempType::Celsius, [](double x) { return x * (5/4); } },
{ TempType::Fahrenheit, [](double x) { return x * (9/4) + 32; } },
{ TempType::Kelvin, [](double x) { return x * (5/4) + 273.15; } },
{ TempType::Rankine, [](double x) { return x * (9/4) + 491.67; } },
{ TempType::Delisle, [](double x) { return (80 - x) * (15/8); } },
{ TempType::Newton, [](double x) { return x * (33/80); } },
{ TempType::Romer, [](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); } },
{ TempType::Fahrenheit, [](double x) { return (x - 7.5) * (24/7) + 32; } },
{ TempType::Kelvin, [](double x) { return (x - 7.5) * (40/21) + 273.15; } },
{ TempType::Rankine, [](double x) { return (x - 7.5) * (24/7) + 491.67; } },
{ TempType::Delisle, [](double x) { return (60 - x) * (20/7); } },
{ TempType::Newton, [](double x) { return (x - 7.5) * (22/35); } },
{ TempType::Reaumur, [](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;

}

我使用 map 的键(TempType),然后将参数传递给 lambda。返回的结果始终为 0。有什么想法吗?

我正在使用 QTCreator 和 mingw。

最佳答案

在C++中4/5,21/40,5/6等都是0因为除法是整数算术。

您需要改为编写 4./5

关于C++ 无法从映射中调用 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48913048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com