【发布时间】:2011-03-26 16:40:12
【问题描述】:
我想对 std::vector 的项目求和
例如
std::vector<int > MYvec;
/*some push backs*/
int x=sum(MYVec); //it should give sum of all the items in the vector
sum函数怎么写?
我试过了
int sum(const std::vector<int> &Vec)
{
int result=0;
for (int i=0;i<Vec.size();++i)
result+=Vec[i];
return result;
}
但是我不喜欢我的方法
【问题讨论】: