I'm trying to iterate through a forward_list manually, and erase certain elements.
My question is essentially the same as this one, but with a forward_list caveat.
The following code (for example only), despite not revisiting before after calling "erase after", does not work (it produces endless garbage).
#include <iostream>
#include <forward_list>
typedef std::forward_list<int>::iterator IT;
int main()
{
    std::forward_list<int> m{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    IT before = m.before_begin();
    for ( IT it = m.begin(); it != m.end(); ++it )
    {
        std::cout << *it;
        if ( *it % 2 == 0 )
        {
            m.erase_after( before );
        }
        before = it;
    }
    for( int i : m )
    {
        std::cout << i;
    }
}
 
     
    