• Create Account

### #ActualSyerjchep

Posted 13 June 2013 - 09:24 PM

Err, I probably should have changed the naming a bit so it was more clear..  The auto type in the examples I gave will become references to the dereferenced iterator.  So, 'auto& it' actually becomes 'whoKnows& it = *impliedIterator;'  I should have changed "it" to be something like "yourObjRef" since the iterator is now implied and hidden.

So right now I'm using alleightup's suggestion and it's working well. Hopefully there isn't any memory loss, it'll be a little bit before I have the actual program ready to test and can see for myself.

Currently my question is how can I remove an element?
Something along the lines of this:

for(auto& it : lifeforms)
{
if(it->food < 1)
lifeforms.remove(it);
}

Does not work.
The program crashes.

How do I remove an element?
(while freeing its memory, as I only need the elements that are in the list)

Given the use of std::forward_list, you are limited to pretty much a single solution for the above:

  lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } );


Sorry, you can't iterate and remove from a forward list directly, for that you need to go back to std::list.

So that seems like something I only need to run once per frame, and not once per organism per frame, correct?
e.g.
while(running)
{
//do other program stuff
for(auto& it : lifeforms)
{
it->doLivingStuff(); //example function, though everything here is pretty much for example
//everything else a lifeform has to do per frame
}
lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } ); //remove any dead lifeforms
}


### #1Syerjchep

Posted 13 June 2013 - 09:23 PM

Err, I probably should have changed the naming a bit so it was more clear..  The auto type in the examples I gave will become references to the dereferenced iterator.  So, 'auto& it' actually becomes 'whoKnows& it = *impliedIterator;'  I should have changed "it" to be something like "yourObjRef" since the iterator is now implied and hidden.

So right now I'm using alleightup's suggestion and it's working well. Hopefully there isn't any memory loss, it'll be a little bit before I have the actual program ready to test and can see for myself.

Currently my question is how can I remove an element?
Something along the lines of this:

for(auto& it : lifeforms)
{
if(it->food < 1)
lifeforms.remove(it);
}

Does not work.
The program crashes.

How do I remove an element?
(while freeing its memory, as I only need the elements that are in the list)

Given the use of std::forward_list, you are limited to pretty much a single solution for the above:

  lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } );


Sorry, you can't iterate and remove from a forward list directly, for that you need to go back to std::list.

So that seems like something I only need to run once per frame, and not once per organism per frame, correct?
e.g.

while(running)
{
//do other program stuff
for(auto& it : lifeforms)
{
it->doLivingStuff(); //example function, though everything here is pretty much for example
//everything else a lifeform has to do per frame
}
lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } ); //remove any dead lifeforms
}

PARTNERS