C
chsalvia
I have a question about the design of STL vector. One thing I wonder
was why the STL designers chose to have the insert() and erase()
functions take an iterator as the first argument, rather than simply
an array index integer referring to a position in the array.
The reason I wonder this is because firstly, the implementation will
need to convert the iterator to an array index integer anyway, because
the iterator may become invalidated if a reallocation occurs, and
secondly, it's easier to simply type something like vec.erase(5)
rather than vec.erase(vec.begin() + 5).
I suppose the reason they did it was so that you can easily swap a
std::vector with an std::list or something else in your code without
breaking the code. But does anyone agree with me that it makes more
sense for a vector, at least, to take an array index integer rather
than iterator for erase/insert, particularly because the iterator may
become invalidated anyway after the operation?
was why the STL designers chose to have the insert() and erase()
functions take an iterator as the first argument, rather than simply
an array index integer referring to a position in the array.
The reason I wonder this is because firstly, the implementation will
need to convert the iterator to an array index integer anyway, because
the iterator may become invalidated if a reallocation occurs, and
secondly, it's easier to simply type something like vec.erase(5)
rather than vec.erase(vec.begin() + 5).
I suppose the reason they did it was so that you can easily swap a
std::vector with an std::list or something else in your code without
breaking the code. But does anyone agree with me that it makes more
sense for a vector, at least, to take an array index integer rather
than iterator for erase/insert, particularly because the iterator may
become invalidated anyway after the operation?