C++ for each, pulling from vector elements

前端 未结 4 1544
长情又很酷
长情又很酷 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:29

    C++ does not have the for_each loop feature in its syntax. You have to use c++11 or use the template function std::for_each.

    struct Function {
        int input;
        Function(int input): input(input) {}
        void operator()(Attack& attack) {
            if(attack->m_num == input) attack->makeDamage();
        }
    };
    Function f(input);
    std::for_each(m_attack.begin(), m_attack.end(), f);
    

提交回复
热议问题