Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 5
    You need access to the container, however. Commented May 20, 2012 at 1:23
  • 14
    @xeo: Well, of course. It would be a gaping hole in const safety if you could do this without a non-const reference to the container. Commented May 20, 2012 at 1:46
  • 3
    +1. Ultra-pedantry: this works for all standard containers, since all standard containers are either sequences or associative containers or unordered associative containers. But erase isn't actually part of the container requirements, so it needn't necessarily work for all user-defined types that satisfy the container requirements. You've sort of already said this in the answer, but add "unordered associative" to the list in parens. Maybe this pedantry should be applied to your comment on Visage's answer, where you said "all containers", more than on your full answer. Commented May 20, 2012 at 15:51
  • 1
    @SteveJessop: Good point. I added unordered associative containers; I forgot that they aren't really "associative containers." Commented May 20, 2012 at 19:39
  • 1
    It should be noted that the erase invocation implies potential iterators and references invalidation for some containers. Of course it shouldn't happen for empty ranges, but certain b̶r̶a̶i̶n̶-̶d̶e̶a̶d̶ implementations like VS2017's one can trigger an assertion error. Commented Mar 12, 2018 at 15:38