I have a normal iterator in position in a bidirectional data structure (actually actually). Now I want to perform an operation on the past x elements from my current position. There will always always be at least x elements, although the last x may be the first element of the vector.
I wrote this code
vector<Record>::iterator it = itCurrentRecord; for (unsigned int i = 0; i < length; i++) { (it--)->length = length; }
It is safe? I am worried that when it points to the first element, the last decrement will cause the iterator to point to one before the first element, which is invalid.
If so, how can I rewrite this in a safe way?
thanks
source share