I realize I can get the iterator reference by calling back()
but why not return it with push_back()
as well? Is it for performance reasons? Or is it due to exception safety (similar to why pop_back()
doesn't return the popped value)? In either case, please explain.
Various insert
functions return an iterator for a very simple reason: the caller does not necessarily know how to get an iterator to that element. map::insert
and set::insert
return one because otherwise, the caller would have to search for the element.
When you do a vector::push_back
, you know where the inserted element is. It's --vector.end()
. You don't have to search for it; it's always that location. You can get it in constant time, and pretty quick constant time at that.
So there's really no point in returning something that the user already knows.