C++ for each, pulling from vector elements

Springfox picture Springfox · Feb 22, 2013 · Viewed 273.5k times · Source

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();
        }
    }
}

Answer

user425495 picture user425495 · Feb 22, 2013

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.