C++20 sections · 1024 units
Open in Course

Removing Elements

Shrinking your vector

To remove the last element, call numbers.pop_back();. The vector shrinks by one and the last element disappears. No return value, it's just gone. Before calling pop_back, make sure your vector isn't empty.

Calling on empty causes undefined behavior. Check numbers.size() > 0 first if unsure. pop_back is fast because it only affects the end. Removing from the middle is slower because the vector must shift all following elements.