public member function
<list>

std::list::remove

void remove (const value_type& val);
Remove elements with specific value
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.

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

A similar function, list::remove_if, exists, which allows for a condition other than an equality comparison to determine whether an element is removed.

参数

val
Value of the elements to be removed.
成员类型value_typeis the type of the elements in the container, defined in list as an alias of its first template parameter (T).

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// remove from list
#include <iostream>
#include <list>

int main ()
{
  int myints[]= {17,89,7,14};
  std::list<int> mylist (myints,myints+4);

  mylist.remove(89);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

输出
mylist contains: 17 7 14


复杂度

Linear in container size (comparisons).

迭代器有效性

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

数据竞争

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

异常安全

If the equality comparison between elements is guaranteed to not throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if an exception is thrown, the container is left in a valid state (basic guarantee).

另见