C++20 sections · 1024 units
Open in Course

Inserting and Erasing

Precise modifications

To insert at a position, use insert(). It needs an iterator. Write numbers.insert(numbers.begin() + 2, 99); to insert 99 at index 2. To remove at a position, use erase(). Write numbers.erase(numbers.begin() + 2); to remove element at index 2.

Following elements shift left. These are slower than push_back or pop_back because they move elements. Use when you need precise control over positions.