The returned iterator will return the address of the iterator. If you want to return the method of accessing the element, return the iterator itself.
Beware that the vector is not global in order to return an iterator / pointer, but these operations in the vector can invalidate the iterator. For example, adding elements to a vector can move vector elements to another position if the new size () is larger than the reserved memory. Removing the element before this element from the vector will cause the iterator to refer to another element.
In both cases, depending on the implementation of the STL, it is difficult to debug using random errors that happen so often.
EDIT after comment: "yes, I didn’t want to return the iterator a) because its const, and b) is it probably just a local, temporary iterator? - Krakkos'
Iterators are no more or less local or temporary than any other variable, and they can be copied. You can return it, and the compiler will make a copy for you, as it will with a pointer.
Now with a constant. If the caller wants to make changes through the returned element (be it a pointer or an iterator), you must use a non-constant iterator. (Just remove the 'const_' from the iterator definition).
David Rodríguez - dribeas Mar 13 '09 at 9:03 2009-03-13 09:03
source share