# (C++) Answer of exercise #9: No for-loops #32

This is the answer of Exercise #9: No for-loops.

## Question #32: Sum all persons' money from a std::vector<const Person*>

Replace the for-loop. You will need:

 ``` #include struct Person {   Person(const int money) : m_money(money) {}   int GetMoney() const { return m_money; }   private:   int m_money; }; int SumMoney(const std::vector& v) {   int sum = 0;   const int sz = v.size();   for (int i=0; i!=sz; ++i)   {     sum+=v[i]->GetMoney();   }   return sum; } ```

 ``` #include #include #include #include #include struct Person {   Person(const int money) : m_money(money) {}   int GetMoney() const { return m_money; }   private:   int m_money; }; int SumMoney(const std::vector& v) {   return std::accumulate(     v.begin(),     v.end(),     static_cast(0),       boost::bind(std::plus(),_1,         boost::bind(&Person::GetMoney,boost::lambda::_2))); } ```

 ``` #include #include struct Person {   Person(const int money) : m_money(money) {}   int GetMoney() const { return m_money; }   private:   int m_money; }; int SumMoney(const std::vector& v) {   return std::accumulate(     v.begin(),v.end(),0,     [](int& sum,const Person* p)     {       return sum + p->GetMoney();     }   ); } ```