remove
语法:
1 |
void remove( const TYPE &val ); |
remove()函数删除链表中所有值为val的元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
源代码 /** * @brief Remove all elements equal to value. * @param value The value to remove. * * Removes every element in the list equal to @a value. * Remaining elements stay in list order. Note that this * function only erases the elements, and that if the elements * themselves are pointers, the pointed-to memory is not * touched in any way. Managing the pointer is the user's * responsibilty. */ void remove(const _Tp& __value); template<typename _Tp, typename _Alloc> void list<_Tp,_Alloc>:: remove(const value_type& __value) { iterator __first = begin(); iterator __last = end(); while (__first != __last) { iterator __next = __first; ++__next; if (*__first == __value) _M_erase(__first); __first = __next; } } |
测试代码
测试结果
10
8
9 8 7 6 4 3 2 1