I am using std::generate to construct a struct and push into a vector. In order to construct the struct, I need to refer to the previous struct in the sequence. Is there any way to do this in c++20 ? I would rather not use boost or a 3rd party library.
Thank you!
std::vector<CashFlow> cashflows(n);
//this is i = 0 struct
CashFlow c;
c.month = 0;
c.begin_balance = initial_prin;
c.end_balance = initial_prin;
cashflows.push_back(c);
std::generate(begin(cashflows) + 1, end(cashflows), [&, i = 1]() mutable {
CashFlow c;
c.month = i;
//here I need end_balance member of the previous struct in the vector
c.begin_balance = std::prev(cashflows.end()).end_balance; // this does not work
c.payment = payment;
c.end_balance = initial_prin * ((c_n - pow(1 + coupon, i)) / (c_n - 1));
c.interest = coupon * c.begin_balance;
c.prin_repayment = payment - c.interest;
return c;
});