stl算法之replace,replace_if,replace_copy,replace_copy_if

1.replace

自己实现源码

template<typename iteratorty,typename type>
void replace_ty(iteratorty first, iteratorty last, const type & oldvalue, const type & newvalue)
{
	for (; first != last;first++)
	{
		if (*first == oldvalue)
		{
			//将所有等于oldvalue的全部替换为newvalue
			*first = newvalue;
		}
	}
}

使用示例:

void main()
{
	std::vector<int> m_vc{ 1, 2, 3, 2, 5, 6 };
	cout << "替换前:\n";
	for_each(m_vc.begin(), m_vc.end(), [](int x){cout << x << " "; });
	cout << endl;
	//等于2的全部替换为2000
	replace_ty(m_vc.begin(), m_vc.end(), 2, 2000);
	cout << "replace替换(2->2000)后:\n";
	for_each(m_vc.begin(), m_vc.end(), [](int x){cout << x << " "; });
	cout << endl;
}

结果:
在这里插入图片描述

2.replace_if

将满足条件的全部替换为设置的值

void main()
{
//大于4的全部替换为1000
	std::vector<int> m_vc{ 1, 2, 3, 4, 5, 6, 7 };
	cout << "替换前:";
	for (auto node :m_vc)
	{ 
		cout << node << " ";
	}
	cout << endl;
	int n = 4;
	cout << "replace_if(替换大于4的元素)后:";
	replace_if(m_vc.begin(), m_vc.end(), [n](int x){return x > n; },1000);
	for_each(m_vc.begin(), m_vc.end(), [](int x){cout << x << " "; });
	cout << endl;
system("pause");
}

结果:
在这里插入图片描述

3.replace_copy

源码:

template <class Inputerator, class Outputerator, class T>
Outputerator replace_copy(ForwardIterator first, ForwardIterator last, Outputerator result, const T & old_value, const T& new_value)
{//范围内所有等于old_value者都以new_value放置新区域
//不符合者原值放入新区域
    for( ; first != last; ++first, ++result)
    {
        *result = *first == old_value ? new_value : *first;
    }
}

将[First, Last]区间中的元素复制到_Dest为起点的目标区,同时将其中与_oldval相等的所有元素替换为_Newval

示例:

void main()
{
std::vector<int> m_vvvc{1,2,3,4,2,6,7};
	std::vector<int> m_oldvc(7,0);//一定要提前有空间,要不报错

	replace_copy(m_vvvc.begin(), m_vvvc.end(), m_oldvc.begin(), 2, 200);
	
	cout << "使用replace_copy后:\n";
	cout << "m_vvvc:";
	for_each(m_vvvc.begin(), m_vvvc.end(), [](int x){cout << x << " "; });
	cout << endl;

	cout << "m_oldvc:";
	for_each(m_oldvc.begin(), m_oldvc.end(), [](int x){cout << x << " "; });
	cout << endl;
	}

结果:
在这里插入图片描述

4.replace_copy_if

将满足某一条件的所有元素都替换了

void main()
{
std:vector<int> m_vcx{ 1, 2, 3, 4, 5, 6, 7, 8 };
	std::vector<int> m_oldvcx(8,0);
	replace_copy_if(m_vcx.begin(), m_vcx.end(), m_oldvcx.begin(), [](int x){return x > 4; }, 2021);
	cout << "使用replace_copy_if(大于4的全部替换为2021)后:\n";
	cout << "m_vcx:";
	for_each(m_vcx.begin(), m_vcx.end(), [](int x){cout << x << " "; });
	cout << endl;

	cout << "m_oldvcx:";
	for_each(m_oldvcx.begin(), m_oldvcx.end(), [](int x){cout << x << " "; });
	cout << endl;
	
}

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发如雪-ty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值