remove()函数stl_list.remove_if()函数的示例| C ++ STL

remove()函数stl

Example:

例:

Here, we have a list of the integers and performing remove operations based on following given test conditions:

在这里,我们有一个整数列表,并根据以下给定的测试条件执行删除操作:

  1. Remove all negative elements

    删除所有负面因素

  2. Remove all elements which are divisible by 11

    删除所有可被11整除的元素

  3. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值