Content deleted Content added
Link suggestions feature: 3 links added. |
Tags: Mobile edit Mobile web edit |
||
Line 97:
=== C++ ===
In C++, a [[const-correctness|const-correct]] implementation of <code>
<syntaxhighlight lang="cpp">
class
public:▼
items{items} {}
std::vector<
}▼
const std::vector<Item>& items() const {
}
double computeTotalCost() const {
▲ private:
return std::ranges::accumulate(
▲ std::vector<Item> items_;
items | std::views::transform([](const Merchandise& m) -> double { return m.getPrice(); }), 0.0
);
}
};
</syntaxhighlight>
Line 119 ⟶ 128:
<syntaxhighlight lang="cpp">
class
public:▼
explicit Cart(std::vector<Merchandise> items): items{items} {}
const std::vector<
return ▲ return *total_cost_;
}
int
totalCost = std::ranges::accumulate(
items | std::views::transform([](const Merchandise& m) -> double { return m.getPrice(); }), 0.0
);
}
}
▲ return total_cost;
▲ }
▲ private:
▲ mutable std::optional<int> total_cost_;
};
</syntaxhighlight>
|