An iterator marks a position in a container. begin() is the first element and end() is one past the last, so a range is half-open: [begin, end). You move with ++it and read with *it, and for a vector you can do arithmetic: v.end() - 3 is three from the end, and it - v.begin() is an index.
Two patterns come up all the time:
- Removing while keeping order. Erasing shifts the later elements left, so a loop that also steps forward skips the element that slid into place. C++20's
std::erase_if(v, pred) does it in one pass; before C++20, v.erase(std::remove_if(...), v.end()) does the same.
- Searching sorted data.
std::lower_bound(first, last, x) finds the first element not less than x by binary search; std::upper_bound finds the first one greater than x.
Erasing or inserting can invalidate iterators into a vector, so do not keep an old one across such a call.
Binary search for a position, then remove by a rule.