Suppose I have a c++ std::list object containing a few elements and an iterator it to one of the elements. How can I remove this element from the list but still be able to access it? In other words, I want it delinked from the list, but not deallocated.
Curiously, using list::erase does seem to achieve this:
#include <iostream>
#include <cstdio>
#include <list>
using namespace std;
int main() {
    std::list<int> a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    auto it = (--a.end());
    a.erase(it);
    cout << *it << endl;   /// outputs '3' anyway, without any error
}
But I assume this isn't really safe?
Questions:
- Is it a guarantee behavior that an element erased by - list::eraseis still accessible by the original iterator?
- If yes, how can I deallocate its memory? 
- If no, how can I erase some element without deallocating its memory and be able access it after erasal? 
 
    