| Yeah - from what I can see, neither interface looks like idiomatic C++. EDIT: Looks like you beat me to the punch on some of these ;) Instead of: float getSum(marketBasket mb) {
float retVal = 0;
// Implement solution here
// ---------
marketBasket::iterator iter = mb.begin();
while (iter.hasNext()) {
retVal += iter.get().price;
iter.next();
}
// ---------
return retVal;
}
I'd rather see real SC++L compatible iterators (as hopefully taught) and saner naming: float getSum(marketBasket market) {
float sum = 0;
// Implement solution here
// ---------
for (marketBasket::iterator item = market.begin(); item != market.end(); ++item) {
sum += item->price;
}
// ---------
return sum;
}
And instead of being pre-provided with a function <void(item)>, if I'm reading the pdf correctly: float getSum(marketBasket mb) {
float retVal = 0;
// Implement solution here
// ---------
function <void(item)> func = [&](item theItem) {
retVal += theItem.price;
};
mb.iterateOverItems(func);
// ---------
return retVal;
}
I'd rather see: float getSum(marketBasket market) {
float sum = 0;
// Implement solution here
// ---------
market.for_each([&](item theItem) {
sum += theItem.price;
});
// ---------
return sum;
}
Or venturing into the far more functional style, where lambdas start to shine for me, personally: float getSum(std::vector<item> market) {
// Implement solution here
// ---------
return std::accumulate(market.begin(), market.end(), 0.0f, [&](float sum, item theItem) {
return sum + theItem.price;
});
// ---------
}
It looks a bit better in C# where your selection of standard functions is a little less anemic and a little nicer to use: float GetSum(MarketBasket market) {
// Implement solution here
// ---------
return market.Sum(item => item.Price);
// ---------
}
|
Shameless plug: http://duneroadrunner.github.io/SaferCPlusPlus/#msevector