C++ for each, pulling from vector elements

前端 未结 4 1549
长情又很酷
长情又很酷 2020-12-23 15:39

I am trying to do a foreach on a vector of attacks, each attack has a unique ID say, 1-3.

The class method takes the keyboard input of 1-3.

4条回答
  •  太阳男子
    2020-12-23 16:24

    This is how it would be done in a loop in C++(11):

       for (const auto& attack : m_attack)
        {  
            if (attack->m_num == input)
            {
                attack->makeDamage();
            }
        }
    

    There is no for each in C++. Another option is to use std::for_each with a suitable functor (this could be anything that can be called with an Attack* as argument).

提交回复
热议问题