第29347题 单选题
下列关于C++遍历数组求和的循环边界处理与优化的说法中,正确的是?

已知以下代码意图计算vector<int> arr中所有元素的和,但存在边界错误:

int calcSum(const vector<int>& arr) {
    int sum = 0;
    for(int i = 0; i <= arr.size(); ++i) {
        sum += arr[i];
    }
    return sum;
}

请选择正确的处理与优化方案:

{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析