Question: This Code is giving error Heap-Use-After-Free

```
for (auto it = mp.begin();it != mp.end();it++)
{
    if(it->second+ttl<=currentTime)
    {
        mp.erase(it);
    }
}

```

But this code

```
for (auto it = mp.begin();it != mp.end();)
{
    if(it->second+ttl<=currentTime)
    {
        mp.erase(it++);
    }
    else
    {
        ++it;
    }
}

```

Does Not give ans error.
Can you please explain why?
Both look basically the same to me.
Please Help

Answer: 
The first one erases the element at `it` which invalidates `it` in all standard containers. Doing `it++` afterwards makes the program have undefined behavior.

The second one *may* work depending on which container you use.

* If you for example use a `*map`, it's ok because `it++` returns the iterator's current value but steps it internally to point at the next element. That iterator will be unaffected by the `erase`.
* If you use a `vector` it has undefined behavior because the iterator after `it++` will be invalidaded by the `erase`.

To make it work with any type of standard container, assign the iterator returned by `erase` to `it`.

```cpp
for (auto it = mp.begin(); it != mp.end();) {
    if (it->second + ttl <= currentTime) {
        it = mp.erase(it);
    } else {
        ++it;
    }
}

```

If you have an `unordered_map`, then since C++20, I recommend using  

[`std::erase_if (std::unordered_map)`](https://en.cppreference.com/w/cpp/container/unordered_map/erase_if) instead:

```cpp
std::erase_if(mp,
    [&](const auto& data) { return data.second + ttl <= currentTime; });

```
In the first case, after erase, 'it' becomes `mp.end()` for map or undefined for `unordered_map`, giving the error.
And in the second case, uses `mp.erase(it++)`, which makes 'it' points to next element in `mp` after erase.