Possible Duplicate:
sum of elements in astd::vector
I want to sum the items of a std::vector
For example
std::vector<int > MYvec;
/*some push backs*/
int x=sum(MYVec); //it should give sum of all the items in the vector
How to write sum
function?
I have tried this
int sum(const std::vector<int> &Vec)
{
int result=0;
for (int i=0;i<Vec.size();++i)
result+=Vec[i];
return result;
}
However I don't like my approach