remove()函数stl
Example:
例:
Here, we have a list of the integers and performing remove operations based on following given test conditions:
在这里,我们有一个整数列表,并根据以下给定的测试条件执行删除操作:
Remove all negative elements
删除所有负面因素
Remove all elements which are divisible by 11
删除所有可被11整除的元素
Remove all elements which are greater than 20
删除所有大于20的元素
Program:
程序:
#include <iostream>
#include <list>
using namespace std;
//function to display the list
void dispList(list<int> L)
{
//declaring iterator to the list
list<int>::iterator l_iter;
for (l_iter = L.begin(); l_iter != L.end(); l_iter++)
cout<< *l_iter<< " ";
cout<<endl;
}
int main()
{
//declaring a list
list<int> iList = {10, 20, 11, 22, 21, -10, -20, 13, 55, 44};
//printing list elements
cout<<"List elements are"<<endl;
dispList(iList);
//remove only negative numbers
iList.remove_if([](int n) {return (n<0); });
cout<<"List elements after removing Negative elements"<<endl;
dispList(iList);
//remove the elements which are divisible by 11
iList.remove_if([](int n) {return (n%11==0); });
cout<<"List elements after removing divisble by 11"<<endl;
dispList(iList);
//remove the elements which are greater than 20
iList.remove_if([](int n) {return (n>20); });
cout<<"List elements after removing greater than 20"<<endl;
dispList(iList);
return 0;
}
Output
输出量
List elements are
10 20 11 22 21 -10 -20 13 55 44
List elements after removing Negative elements
10 20 11 22 21 13 55 44
List elements after removing divisble by 11
10 20 21 13
List elements after removing greater than 20
10 20 13
翻译自: https://www.includehelp.com/stl/examples-of-list-remove-if-function.aspx
remove()函数stl