我遇到的问题是我不确定如何将我的#include <iostream>
#include <memory>
struct Base
{
virtual void print()=0;
virtual ~Base(){}
};
struct Derived: public Base
{
void print(){std::cout << "this is Derived\n";}
};
struct Foo
{
Foo(std::unique_ptr<Base>&& r): bref{std::move(r)} {}
void print(){
bref->print();
}
std::unique_ptr<Base> bref;
};
int main()
{
Foo f{std::unique_ptr<Derived>(new Derived)}; //or make_unique
f.print();
}
数组转换为double
。我有一些非常笨拙的LINQ Dictionary
语句,因此我想使用Where
的键来查找特定值。我有两个类,分别是getter和setter,然后是计算器。
我在尝试进行查找或Dictionary
时花了些力气,但没有任何运气。
Dictionary
答案 0 :(得分:1)
您可以在循环之前按OriginYear
和DevelopmentYear
对产品进行分组。这样的事情可能会有所帮助:
public IList<double> Calculate(Product product)
{
IList<double> cumulativeDataTriangle = new List<double>();
if (!product.ProductIncrementalValues.Any())
return cumulativeDataTriangle;
var lookup = product.ProductIncrementalValues.ToLookup(v => (v.OriginYear, v.DevelopmentYear), v => v.IncrementalValue);
for (int i = 0; i < product.NoOfDevelopmentYears; i++)
{
var originYear = product.EarliestOriginYear + i;
double previous = 0;
for (int j = 0; j < product.NoOfDevelopmentYears - i; j++)
{
var developmentYear = originYear + j;
var incrementalValues = lookup[(originYear, developmentYear)];
double incrementalValue = incrementalValues.FirstOrDefault();
var tmp = incrementalValue + previous;
cumulativeDataTriangle.Add(tmp);
previous = tmp;
}
}
return cumulativeDataTriangle;
}
答案 1 :(得分:0)
由于上一个答案,我设法基于Dictionary
和OriginYear
实现了DevelopmentYear
,并返回了IncrementalValue
。
public IList<double> Calculate(Product product)
{
IList<double> cumulativeDataTriangle = new List<double>();
if (!product.ProductIncrementalValues.Any())
return cumulativeDataTriangle;
var lookup = product.ProductIncrementalValues.
ToDictionary(v => (v.OriginYear, v.DevelopmentYear), v => v.IncrementalValue);
for (int i = 0; i < product.NoOfDevelopmentYears; i++)
{
var originYear = product.EarliestOriginYear + i;
double previous = 0;
for (int j = 0; j < product.NoOfDevelopmentYears - i; j++)
{
var developmentYear = originYear + j;
double incrementalValue;
lookup.TryGetValue((originYear, developmentYear), out incrementalValue);
var tmp = incrementalValue + previous;
cumulativeDataTriangle.Add(tmp);
previous = tmp;
}
}
return cumulativeDataTriangle;
}