For next examples assumed that you use C++11. Example with ranged-based for loops:
for (auto &attack : m_attack) // access by reference to avoid copying { if (attack->m_num == input) { attack->makeDamage(); } }
You should use const auto &attack
depending on the behavior of makeDamage()
.
You can use std::for_each
from standard library + lambdas:
std::for_each(m_attack.begin(), m_attack.end(), [](Attack * attack) { if (attack->m_num == input) { attack->makeDamage(); } } );
If you are uncomfortable using std::for_each
, you can loop over m_attack
using iterators:
for (auto attack = m_attack.begin(); attack != m_attack.end(); ++attack) { if (attack->m_num == input) { attack->makeDamage(); } }
Use m_attack.cbegin()
and m_attack.cend()
to get const
iterators.