可能重复:
a中元素的总和std::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
Run Code Online (Sandbox Code Playgroud)
怎么写sum功能?
我试过这个
int sum(const std::vector<int> &Vec)
{
int result=0;
for (int i=0;i<Vec.size();++i)
result+=Vec[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
但是我不喜欢我的做法