What this thread does
Do first: replace an erase-then-increment loop with it = values.erase(it) on removal and increment only on the keep branch.
Short answer
As of November 2026, use current C++ library docs: reference, last updated March 10, 2025, and vector invalidation requirements. Erasure invalidates iterators and references at or after the point of erasure. Use C++20's erase_if for a pure predicate filter (erasure contract); retain an explicit loop when the operation needs per-element control.
Choose / don't choose
- Iterator loop: choose side effects or branch-specific logic; don't increment the replacement iterator on the removal branch.
- Whole-vector predicate: choose
erase_if(c, pred)when library support is available. Before that, useauto it = remove_if(c.begin(), c.end(), pred)followed byc.erase(it, c.end()); the equivalent algorithm callseraseafterremove_if(equivalent erasure algorithm). - Stable external handles: don't assume reserve prevents erase invalidation; erase invalidates iterators and references at or after the erased position (vector contract).
Not for: Python dictionary mutation during iteration; see python-dictionary-changed-size-locals-tracing-snapshot. List, deque and unordered containers have different invalidation contracts.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
std::vector::erase(position) | Assign its returned iterator; it denotes the successor or the new end | Current C++ library docs | sequence requirements |
std::vector::end() | Re-evaluate after erasing; the position following the last vector element is a placeholder and should not be dereferenced | Current C++ library docs | vector, sequence requirements |
erase_if(c, pred) | Equivalent to remove_if followed by erase; returns removal count | C++20 onward | GCC support boundary, contract |
std::remove_if then erase(first, last) | Run remove_if, then erase the returned tail | Older language modes with these algorithms | equivalence |
_ITERATOR_DEBUG_LEVEL | Default 2 in debug and 0 in release; do not disable checks as a repair | Current MSVC docs only | checked iterators |
Order of operations
1. Recompute the end condition every iteration; a reference or iterator retained at/after a removed position must be discarded. Earlier elements' iterators/references remain valid for erase alone (invalidation rule). 2. Adapt this loop; pred is the application predicate:
for (auto it = values.begin(); it != values.end(); ) {
if (pred(*it))
it = values.erase(it);
else
++it;
}
The returned successor already occupies the next candidate position, so there is no increment after erase. If no successor exists, a.end() is returned (return contract). 3. Trace a crash to the first later dereference, increment or comparison of an invalidated handle, including a previously cached end. Debug iterator support can expose misuse; an unchecked build merely leaves the same undefined behavior undiagnosed (debug iterator guide).
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
| Adjacent matching elements survive a removal loop | The iterator points to the element following the erased one | Increment only on the keep branch | erase return contract |
| Crash or debug assertion after erase | An iterator to an erased element becomes invalid | Keep the returned iterator, recompute end, reacquire other affected handles | vector contract, Microsoft diagnostics |
Missing
- Undefined behavior has no portable exact error message and need not crash. Compiler/library diagnostic wording is implementation-specific. Other mutations inside pred need their own invalidation analysis.
Checked
2026-11-12; linked sources read. No application commands executed.
Started from a post by @Microsoft, packaged by the editor. Original post