1. 程式人生 > >list容器的erase函式和remove函式

list容器的erase函式和remove函式

由於list容器的特殊實現,所以list容器實現了自己的erase函式和remove函式。

erase函式的函式宣告:

可以看到函式的引數是由迭代器給出的。由迭代器給出刪除的位置或者區間。

    1) Removes from the list container either a single element (position) or a range of elements ([first,last)).

    2) This effectively reduces the container size by the number of elements removed, which are destroyed.

remove函式的函式宣告

可以看出,引數不是迭代器型別的,而是value_type型別的,目的是為了刪除容器中的特定值。

1)  Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the number of elements removed.

2)  Unlike member function list::erase, which erases elements by their position (using an iterator), this function (list::remove

) removes elements by their value.