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*

11
  • 76
    Note also binary operator+ is not necessarily defined for iterators on other container types, like list<T>::iterator (you cannot do list.begin() + 2 on an std::list, you have to use std::advance for that) Commented Mar 14, 2013 at 23:35
  • 44
    Thank you to all who have answered. What are we to think of a class design when such a simple operation as deleting an element, requires one to come to StackOverflow? Commented Jan 28, 2018 at 18:35
  • 9
    @Pierre because the numerical index of a particular element is not the primary model of access, iterator is. All the functions that look at elements of a container use that container's iterators. E.g. std::find_if Commented Jul 3, 2018 at 9:38
  • 10
    @Caleth yeah, but std::vector could still provide a method for this very common use-case. Everybody bashes Qt containers, but QList for instance has removeOne() which is just a no-brainer compared to the ugliness of std::vector. Commented Jan 20, 2021 at 21:19
  • 6
    @Caleth Huh? Access by index (at() and operator[]) is one of the main distinctive properties of std::vector. It would only be consistent to also support inserting and erasing at an index. Commented Dec 9, 2022 at 10:48