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