Can you remove elements from a std::list while iterating through it?

后端 未结 13 881
长情又很酷
长情又很酷 2020-11-22 06:30

I\'ve got code that looks like this:

for (std::list::iterator i=items.begin();i!=items.end();i++)
{
    bool isActive = (*i)->update();
    /         


        
13条回答
  •  遥遥无期
    2020-11-22 06:36

    I think you have a bug there, I code this way:

    for (std::list::iterator itAudioChannel = audioChannels.begin();
                 itAudioChannel != audioChannels.end(); )
    {
        CAudioChannel *audioChannel = *itAudioChannel;
        std::list::iterator itCurrentAudioChannel = itAudioChannel;
        itAudioChannel++;
    
        if (audioChannel->destroyMe)
        {
            audioChannels.erase(itCurrentAudioChannel);
            delete audioChannel;
            continue;
        }
        audioChannel->Mix(outBuffer, numSamples);
    }
    

提交回复
热议问题