remove/erase from std::list
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/180516/how-to-filter-items-from-a-stdmap">How to filter items from a std::map?</a><br>
  <a href="http://stackoverflow.com/questions/12666869/stdlisterase-not-working">std::list::erase not working</a>  </p>
</blockquote>



<p>I have some silly questions regarding remove, erase in std::list.</p>

<p>I have a class defined as:</p>

<pre><code>class CBase
{
public:
    CBase(int i): m(i)
    {};

    int m;
};
</code></pre>

<p>then, I set it up as:</p>

<pre><code>list&lt;CBase&gt; ml;
CBase b1(1);
CBase b2(2);
CBase b3(3);
CBase b4(4);
ml.push_back(b1);
ml.push_back(b2);
ml.push_back(b3);
ml.push_back(b4);
</code></pre>

<p>I can erase the item which has m==2 by;</p>

<pre><code>for (list&lt;CBase&gt;::iterator it=ml.begin(); it!=ml.end(); ++it)
{
    if (it-&gt;m == 2)
    {
        ml.erase(it--);
    }
}
    // show what we have now:
for (list&lt;CBase&gt;::iterator it=ml.begin(); it!=ml.end(); it++)
{
    cout &lt;&lt; it-&gt;m;
}
</code></pre>

<p>But if I do:</p>

<pre><code>for (list&lt;CBase&gt;::iterator it=ml.begin(); it!=ml.end(); it++)
{
    if (it-&gt;m == 2)
    {
        ml.erase(it);
        it--;
    }
}
</code></pre>

<p>There will be exception. Why is this?</p>

<p>And if I want to remove b3,</p>

<p>ml.remove(b3);</p>

<p>will not compile. All the examples I found online use <code>list&lt;int&gt;</code>, and there is no problem calling <code>mylist.remove(3)</code>, if <code>mylist is list&lt;int&gt;</code>. How can I make it work?</p>
