Search⌘ K

Calculations

Explore how to calculate sums of order values from CSV data using C++17, with techniques to filter by date and suggestions for adding statistics and enhancing code efficiency.

We'll cover the following...

Once all the records are available we can compute their sum:

C++
[[nodiscard]] double CalcTotalOrder(const std::vector<OrderRecord>& records,
const Date& startDate, const Date& endDate)
{
return std::accumulate(std::begin(records), std::end(records), 0.0,
[&startDate, &endDate](double val, const OrderRecord& rec) {
if (rec.CheckDate(startDate, endDate))
return val + rec.CalcRecordPrice();
else
return val;
}
);
}

The code runs on the vector of all records and then calculates the price of each element if they fit between startDate and endDate. Then they are all summed in std::accumulate.

Design Enhancements

...