<分区>
Possible Duplicate:
sum of elements in astd::vector
我想对 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;
}
但是我不喜欢我的做法