C++ for each, pulling from vector elements

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

    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.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-23 16:25

    The for each syntax is supported as an extension to native c++ in Visual Studio.

    The example provided in msdn

    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int total = 0;
    
      vector<int> v(6);
      v[0] = 10; v[1] = 20; v[2] = 30;
      v[3] = 40; v[4] = 50; v[5] = 60;
    
      for each(int i in v) {
        total += i;
      }
    
      cout << total << endl;
    }
    

    (works in VS2013) is not portable/cross platform but gives you an idea of how to use for each.

    The standard alternatives (provided in the rest of the answers) apply everywhere. And it would be best to use those.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题