Deleting last element in a std::vector of std::shared_ptr while iterating over it causes segmentation fault : <p>please try to compile and run the following code.
When iterating over a vector of shared pointers, I have to delete last element, this will result in a segmentation fault, but I can't understand why the <code>for</code> iteration doesn't break when <code>el_it</code> reaches <code>v.end()</code>, and I have to do it manually (commented code).</p>

<pre><code>#include &lt;vector&gt;
using std::vector;

#include &lt;memory&gt;
using std::shared_ptr;

#include &lt;algorithm&gt;
using std::remove;

class A {
public:
    A(int age) { age_ = age; }
    int age_ = 0;
    int alive_ = 1;
};

int main() {

    shared_ptr&lt;A&gt; a0(new A(0));
    shared_ptr&lt;A&gt; a1(new A(1));
    shared_ptr&lt;A&gt; a2(new A(2));
    vector&lt; shared_ptr &lt;A&gt; &gt; v;
    v.push_back(a0);
    v.insert(v.end(), a1);
    v.insert(v.end(), a2);
    for (auto el_it = v.begin(); el_it != v.end(); ++ el_it) {
        auto el = *el_it;
        if (el-&gt;age_ == 2) {
            v.erase(el_it);
        }
        /*
        if (el_it == v.end()) // Why is this required ??
            break;
        */

    }
    return 0;

}

</code></pre>
