std::list<T,Allocator>:: remove, remove_if
| (1) | ||
|
void
remove
(
const
T
&
value
)
;
|
(C++20 前) | |
|
size_type remove
(
const
T
&
value
)
;
|
(C++20 起)
(C++26 起为 constexpr) |
|
| (2) | ||
|
template
<
class
UnaryPred
>
void remove_if ( UnaryPred p ) ; |
(C++20 前) | |
|
template
<
class
UnaryPred
>
size_type remove_if ( UnaryPred p ) ; |
(C++20 起)
(C++26 起为 constexpr) |
|
移除所有满足特定条件的元素。
仅使被移除元素的迭代器和引用失效。
目录参数
返回值
复杂度给定 N 为 std:: distance ( begin ( ) , end ( ) ) :
1)
恰好进行
N
次
operator
==
比较。
2)
恰好执行
N
次谓词
p
的应用。
注释
示例
运行此代码
#include <list> #include <iostream> int main() { std::list<int> l = {1, 100, 2, 3, 10, 1, 11, -1, 12}; auto count1 = l.remove(1); std::cout << count1 << " elements equal to 1 were removed\n"; auto count2 = l.remove_if([](int n){ return n > 10; }); std::cout << count2 << " elements greater than 10 were removed\n"; std::cout << "Finally, the list contains: "; for (int n : l) std::cout << n << ' '; std::cout << '\n'; } 输出: 2 elements equal to 1 were removed 3 elements greater than 10 were removed Finally, the list contains: 2 3 10 -1 缺陷报告以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
参阅
| |||||||||||||||||||||||||||||||||||