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.
push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1. 1.
Yes it is correct that the object is passed by reference to the push_back function. Inside the function a copy is made that is stored in the vector.
The push_back() function is used to insert an element at the end of a vector.
The push_back() method is typically , provided there is adequate capacity in the underlying array.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With