STL Vector question...

I'm still relatively a newbie...but I've gained a decent amount of experience using the STL's vector class. Right now, there are only 4 iterator functions (begin, end, rbegin, and rend). However, on more than one occasion, I've noticed that it would be extremely useful if there were a member function that would return an iterator to a certain index to a vector, rather than just the beginning or end.

For example, say I want to erase the 5th element in my vector of ints. There would be no way for me to call the erase function without an iterator...so I have been resorting to something stupid like this:

vector<int> myVector;
vector<int>::iterator it = myVector.begin();

for (int i = 0; i<5; i++)
it++;

myVector.erase(it);

In other words, making a O(n) algorithm to simply get an iterator to this location to then pass to erase. Is there any other way around this? Why doesn't the STL Vector class come with such a function to convert an index into an iterator...or why don't functions such as erase accept an index?

Thanks in advance


Last edited on
You do know you can simply + and - on iterators and do this:

myVector.erase(myVector.begin()+4);
The problem is that only random access iterators support the idea of adding scalars to them in constant time.

For all other iterators, finding an iterator to the nth element of a container requires essentially OP's for loop, which is O(n).

If you are looking for the fastest, but least general, solution, then use firedraco's solution.

If you are looking for the most general solution, then use std::advance:

1
2
3
4
vector<int>::iterator it( myVector.begin() );
assert( myVector.size() > 5 );
std::advance( it, 5 );
myVector.erase( it );


Did not look to see if it already does, but a slick implementation of advance() would specialize on random access iterators and implement it with firedraco's solution and fall back to the O(n) algorithm for other iterator types.
The problem is that only random access iterators support the idea of adding scalars to them in constant time.


I think that should read: The problem is that only random access iterators support the idea of adding scalars to them.
I stand corrected.
Interesting...however I'm not quite sure I understand the difference between the two proposed methods (draco's method and the std::advance method)...they both appear to do the exact same thing...what's different about them?

Thanks again
Draco's method using operator+() works just with vectors. jsmith's method using std::advance() can be used with other containers as well.
That doesn't seem correct PanGalactic. The example for std::deque does exactly the same thing. Take a look at this.
http://cplusplus.com/reference/stl/deque/erase/
Draco's method using operator+() works just with vectors


Shouldn't it be containers with random access iterators?
Ah -- yes. Good point, kempofighter. I so rarely use deque that I forget that it, too, is a random access container.
Topic archived. No new replies allowed.