Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand vector pop_back implementation?

Tags:

c++

stl

vector

sgi

I am currently thinking why STL implement vector pop_back in this way. Why we move the finish pointer foreword first and then use finish pointer deallocate the space of last element?

void pop_back() {
    --_M_finish;
    destroy(_M_finish);
}
like image 468
Edee Avatar asked Sep 02 '19 09:09

Edee


1 Answers

Most likely _M_finish is the end pointer, that is points to the item just after the last item. After pointer has been moved one step back it will point to the current last item that is going to be deleted. And after that item is deleted _M_finish will continue to point at the same item which is now again is the item just after the last item.

enter image description here

like image 93
user7860670 Avatar answered Oct 31 '22 19:10

user7860670