last elemnt in the vector is a sum of the previous ones c++ code example Example: sum elements in vector c++ for (auto& n : vector) sum_of_elems += n;