473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about STL Vector design

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?

Oct 12 '07 #1
3 2854
ch******@gmail.com wrote:
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.

--
Ian Collins.
Oct 12 '07 #2
On Oct 12, 1:55 pm, chsal...@gmail.com wrote:
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

Oct 12 '07 #3
On 2007-10-12 19:55, ch******@gmail.com wrote:
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<T>::iterator it = vec.begin() + index;
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.

--
Erik Wikström
Oct 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? ...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
31
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
3
by: Alex | last post by:
Hi everyone, I have a question that implies thinking about some design issues with C ++. I have a method like this: class C { .... public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.