public member function
<forward_list>

std::forward_list::erase_after

iterator erase_after (const_iterator position);iterator erase_after (const_iterator position, const_iterator last);
Erase elements
Removes from the forward_list container either a single element (the one after position) or a range of elements ((position,last)).

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

与其他标准序列容器不同,listforward_list容器专门设计用于在任何位置(即使在序列中间)高效地插入和删除元素。

参数

position
Iterator pointing to an element in the forward_list container. The (first) element removed is the one after this.
成员类型const_iteratoris a forward iterator type that points to elements.
last
Iterator pointing to the element after the last one to be removed. The range of elements removed is the open interval(position,last), which includes all the elements between position and last, but not position nor last themselves.
成员类型const_iteratoris a forward iterator type that points to elements.

返回值

An iterator pointing to the element that follows the last element erased by the function call, which is last for the second version.
If the operation erased the last element in the sequence, the value returned is end.

成员类型iteratoris a forward iterator type that points to elements.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// erasing from forward_list
#include <iostream>
#include <forward_list>

int main ()
{
  std::forward_list<int> mylist = {10, 20, 30, 40, 50};

                                            // 10 20 30 40 50
  auto it = mylist.begin();                 // ^

  it = mylist.erase_after(it);              // 10 30 40 50
                                            //    ^
  it = mylist.erase_after(it,mylist.end()); // 10 30
                                            //       ^

  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
输出
mylist contains: 10 30


复杂度

Linear in the number of elements erased (destructions).

迭代器有效性

Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and references keep their validity.

数据竞争

The container is modified.
The elements removed are modified. Concurrently accessing or modifying other elements is safe, although iterating ranges that include the removed elements is not.

异常安全

If position (or the range) is valid, the function never throws exceptions (no-throw guarantee).
否则,将导致未定义行为

另见