.pushback (vectors)

Why would I use pushback? I understand how it’s used; but why would I use it?

With vectors the insertion operation happens at the tail-end. push_back(item) is how you insert items into your array. (Note that it’s different than assigning items to a previously existing array slot). Since vectors are dynamic arrays, they will resize automatically after reaching a certain load balance threshold.

A great feature of this is the runtime cost (super-low, constant cost operation or O(1) ).
If you need to be inserting things in a way that might maintain a sorted order then you need to look at more advanced structures.

2 Likes

Gotcha. Thank you so much!