C++: containers and standard algorithms¶
Prefer a standard container and an algorithm that expresses the operation clearly. This example uses C++17.
| sum.cpp | |
|---|---|
Compile and run:
std::accumulate visits each element once: \(O(n)\) time and \(O(1)\) auxiliary space.
The initial value determines the accumulator type; use 0LL when a wider integer
sum is needed.