C++ remove_copy_If()的使用

remove_copy_If()的作用是用于拷贝自己想要类型的值到另外一个容器或者覆盖自身

remove_copy_If()需要用到容器开头,结束,拷贝容器的开头,拷贝条件。
在这里插入图片描述

例子代码如下:

//有效值获取复制
bool bools(int i) {
//取一个值能被二整除的值
	return ((i % 2) == 1);
}

void RemoveCopyIf()
{
//旧容器
	int myattr[] = { 1,2,4,3,8,9,7,5,6,10 };

	//创建一个新容器
	vector<int> myattring(10);

//将myattr里符合条件的数据拷贝到myattring中
	remove_copy_if(myattr, myattr + 10, myattring.begin(), bools);

//用作对比比较
	cout << "1 2 4 3 8 9 7 5 6 10" << endl;
	cout << endl;
//for循环遍历出所有拷贝值
	for (vector<int>::iterator it = myattring.begin(); it != myattring.end(); ++it) 
	{
		cout << *it << ends;
	}
}

void main(){
	RemoveCopyIf();
}

效果如下:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的remove函数是用来从容器中移除指定元素的函数。它并不会真正删除元素,而是通过将其他元素向前移动来覆盖它们。remove函数返回一个迭代器,指向通过移除操作得到的新序列的尾部。可以使用这个迭代器作为被删除序列的开始迭代器来调用容器的成员函数erase()来实际删除元素。例如,可以按照以下方式使用remove函数来移除容器中的0元素: std::deque<double> samples {1.5, 2.6, 0.0, 3.1, 0.0, 0.0, 4.1, 0.0, 6.7, 0.0}; samples.erase(std::remove(std::begin(samples), std::end(samples), 0.0), std::end(samples)); 上述代码会将0元素从samples容器中移除,并且保持原始序列的顺序不变。最后,可以使用copy函数将结果打印出来。 std::copy(std::begin(samples), std::end(samples), std::ostream_iterator<double>{std::cout, " "}); std::cout << std::endl; 执行上述代码会输出移除0元素之后的结果:1.5 2.6 3.1 4.1 6.7。 如果希望保留原始序列并生成一个移除指定元素之后的副本,可以使用remove_copy函数。例如: std::deque<double> samples {1.5, 2.6, 0.0, 3.1, 0.0, 0.0, 4.1, 0.0, 6.7, 0.0}; std::vector<double> edited_samples; std::remove_copy(std::begin(samples), std::end(samples), std::back_inserter(edited_samples), 0.0); 上述代码会将samples中的0元素移除,并将移除后的结果存储在edited_samples中。 总结来说,remove函数是用来移除容器中指定元素的函数,但它并不会真正删除元素,而是通过移动其他元素来覆盖它们。要实际删除元素,可以配合使用erase函数。如果希望保留原始序列并生成移除指定元素之后的副本,可以使用remove_copy函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ removeremove_copyremove_if和remove_copy_if函数使用详解](https://blog.csdn.net/digitalkee/article/details/112447321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++remove()函数总结](https://blog.csdn.net/cnd2449294059/article/details/75948661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值