Efficient way of getting sum of stack elements, without emptying it or copying it to other stack? What I am doing is:
stack<int> si;
int sum = 0;
stack<int> tsi(si);
while (!tsi.empty()) {
sum += tsi.top();
tsi.pop();
}
This can be done using https://ideone.com/i7ha2j midified verson of: https://stackoverflow.com/a/13428630/7267150
Any other way to achieve the requirement (this case, sum)?? I prefer STL (is it possible).