Kodewick
开始学 →
入门→0 / 3

加起来

#001入门vector循环约 3 分钟

写 sum_all(v),把 vector 里所有的数加起来返回。空的就是 0。

示例
sum_all({3, 4, 5})→12
sum_all({})→0
函数int sum_all(const vector<int> &v)
main.cpp
#include <vector>
using namespace std;

int sum_all(const vector<int> &v) {
    int total = 0;
    // 在这里写:用范围 for 把每个数加到 total
    return total;
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。