首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

remove_if

void remove( const T& value );

(since C++11)

template< class UnaryPredicate > void remove_if( UnaryPredicate p );

(since C++11)

移除满足特定条件的所有元素。第一个版本删除所有等于value,第二个版本移除谓词的所有元素。p回报true...

参数

value

-

value of the elements to remove

p

-

unary predicate which returns ​true if the element should be removed. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type forward_list<T,Allocator>::const_iterator can be dereferenced and then implicitly converted to Type. ​

返回值

%280%29

复杂性

容器的大小成线性。

二次

代码语言:javascript
复制
#include <forward_list>
#include <iostream>
 
int main()
{
    std::forward_list<int> l = { 1,100,2,3,10,1,11,-1,12 };
 
    l.remove(1); // remove both elements equal to 1
    l.remove_if([](int n){ return n > 10; }); // remove all elements greater than 10
 
    for (int n : l) {
        std::cout << n << ' '; 
    }
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
2 3 10 -1

二次

另见

removeremove_if

removes elements satisfying specific criteria (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券