C++ for each, pulling from vector elements

70

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.

I am trying to use a foreach to run through my elements in m_attack to see if the number matches, if it does... do something.

The problem I'm seeing is this:

a'for each' statement cannot operate on an expression of type "std::vector<Attack

Am I going about this totally wrong, I have C# experience and is kind of what I'm basing this on, any help would be appreciated.

My code is as follows:

In header

vector<Attack> m_attack;

In class

int Player::useAttack (int input)
{

    for each (Attack* attack in m_attack) // Problem part
    {  
        //Psuedo for following action
        if (attack->m_num == input)
        {
            //For the found attack, do it's damage
            attack->makeDamage();
        }
    }
}

This question is tagged with c++ for-loop vector each

~ Asked on 2013-02-22 15:16:11

The Best Answer is


165

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.

~ Answered on 2013-02-22 15:38:29


26

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).

~ Answered on 2013-02-22 15:19:32


Most Viewed Questions: