Question about STL Vector design

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?
 
I

Ian Collins

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.
Probably because all of the standard algorithms use iterators and not
all of the standard containers have index operators.
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).
Why would it have to perform a conversion? An iterator identifies a
position in a vector.
 
L

Lance Diduck

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?
Sounds reasonable to me, esp for vector and deque. After all map's and
set's erase members are able to take a Key const& as well as an
iterator. vector and deque indexes are very much like keys, so why
shouldnt it be able to take an index? The only difference would be
that after you erased the index (that is not end()-1), an element
could actually still exist at that index (which is unlike a key). But
this behavior is the same as if it were an iterator erase, so I dont
see much objection about "correctness." i.e. even after you do an
iterator erase (that is not next to last), the iterator is still
valid, it just points to something different.
Post your suggestion on comp.std.c++ and see what sort of feedback you
get. They would even tell you how to submit your idea to the standards
committee.
Lance
 
G

Guest

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.

Probably since all the STL containers and algorithms operate on
iterators, it would be a shame to leave vector out of all the good stuff
that the other containers enjoy.
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).

First, the implementation does not have to convert it to an index, just
check if the iterator refers to the current element. And even if you
want to convert it to an index that would not be very hard. Second, the
iterator will only be invalid *after* the call to erase. Third, if you
know the index it is very easy to get an iterator to that element:

std::vector said:
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?

The iterator will become invalid after the operation, always. And no it
does not make sense to make vector a special case when you can just do

vec.erase(vec.begin() + index);

to get what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top