Namespaces
Variants

std::experimental::erase (std::forward_list)

来自 zh.cppreference.net

定义于头文件 <experimental/forward_list>
template< class T, class A, class U >
void erase( std::forward_list<T, A>& c, const U& value );
(library fundamentals TS v2)

从容器中移除所有与 value 比较相等的元素。等价于 c.remove_if([&](auto& elem) { return elem == value; });。

参数

c - 要执行擦除操作的容器
value - 待移除的数值

复杂度

线性。

示例

#include <experimental/forward_list>
#include <iostream>
auto show = [](const auto& container)
{
    for (auto e : container)
        std::cout << e;
    std::cout << '\n';
};
int main()
{
    std::forward_list<int> data{1, 1, 1, 4, 1, 1, 1, 2, 1, 1, 1};
    show(data);
    std::experimental::erase(data, 1);
    show(data);
}

输出:

11141112111
42

注释

与 std::forward_list::remove 不同,此函数模板接受异构类型,且在调用 == 运算符前不会强制转换为容器的值类型。

另请参阅

移除满足特定条件的元素
(函数模板 & 算法函数对象)
移除满足特定条件的元素
(公有成员函数的 std::forward_list<T,Allocator>)
(library fundamentals 2 TS)
从std::forward_list
中移除所有满足谓词的元素(函数模板)