第九章(14):STL之常用拷贝和替换算法

🎉welcome🎉
✒️博主介绍:一名大一的智能制造专业学生,在学习C/C++的路上会越走越远,后面不定期更新有关C/C++语法,数据结构,算法,Linux,ue5使用,制作游戏的心得,和大家一起共同成长。
✈️C++专栏:C++爬塔日记
😘博客制作不易,👍点赞+⭐收藏+➕关注

前情回顾

在上一块石碑中,我学到了如何去使用一些常用的排序算法,同时下一块石碑也显露出来…

常用拷贝算法

  • 拷贝算法通常使用的就只有一种:
copy//将容器指定范围之内的数据拷贝到另一个容器当中

copy

  • copy,翻译过来就是拷贝,它的作用也确实是拷贝,将一个容器内的一个区间内的元素拷贝给另一个容器,注意,要提前给要拷贝进去的容器开辟好空间
copy(beg,end,dest);

使用:

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

void print(vector<int>& a)
{
	for (auto b = a.begin(); b < a.end(); b++)
	{
		cout << *b << " ";
	}
	cout << endl;
}
void test1()
{
	vector<int> a;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
	}
	print(a);
	auto b = a.begin();
	b++; b++; b++; b++;
	vector<int> c;
	c.resize(6);
	copy(b, a.end(), c.begin());
	print(c);
}
int main()
{
	test1();
	return 0;
}

在这里插入图片描述

常用替换算法

  • 替换算法,其实本质是和拷贝是一样的,拷贝也可以属于替换算法,拷贝过去的元素也就是一个区间内的元素把另一个区间内的元素替换掉了,常用的替换算法有三种:
replace//将指定范围内的元素换成新的元素
replace_if//将指定范围内满足条件的元素替换成新的元素
swap//互换两个容器内的元素

replace

  • replace它的作用是将区间内的指定元素替换成新元素
replace(beg,end,T old,T new)
  • beg是区间的开始迭代器,end为区间结束迭代器,old是要被替换的旧元素,new是替换旧元素的新元素

使用:


#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

void print(vector<int>& a)
{
	for (auto b = a.begin(); b < a.end(); b++)
	{
		cout << *b << " ";
	}
	cout << endl;
}
void test1()
{
	vector<int>a;
	for (int i = 0; i < 10; i++)
	{	
		a.push_back(i);
		a.push_back(i);
		a.push_back(i);
	}
	print(a);
	for (int i = 0; i < 10; i++)
		replace(a.begin(), a.end(), i, 1);
	print(a);
}
int main()
{
	test1();
	return 0;
}

在这里插入图片描述

replac_if

  • replace_if的作用与replace的作用基本是一致的,但是他它是满足条件的元素替换成新元素
replace_if(beg,end,_pred,T new)
  • beg是区间的开始迭代器,end为区间结束迭代器,_pred是谓词,满足谓词条件的是要被替换的旧元素,new是替换旧元素的新元素

使用:

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

void print(vector<int>& a)
{
	for (auto b = a.begin(); b < a.end(); b++)
	{
		cout << *b << " ";
	}
	cout << endl;
}
bool big(int a)
{
	return a > 0;
}
void test1()
{
	vector<int>a;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
		a.push_back(i);
		a.push_back(i);
	}
	print(a);
	replace_if(a.begin(), a.end(),big , 1);
	print(a);
}
int main()
{
	test1();
	return 0;
}

在这里插入图片描述

swap

  • swap是交换两个容器内的元素,这里的容器一定是同种的容器
swap(cc1,c2)
  • c1是容器1,c2是容器2

使用:

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

void print(vector<int>& a)
{
	for (auto b = a.begin(); b < a.end(); b++)
	{
		cout << *b << " ";
	}
	cout << endl;
}
bool big(int a)
{
	return a > 0;
}
void test1()
{
	vector<int>a, b;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
		b.push_back(i + 10);
	}
	cout << "交换前:" << endl;
	print(a);
	print(b);
	swap(a, b);
	cout << "交换后:" << endl;
	print(a);
	print(b);
}
int main()
{
	test1();
	return 0;
}

在这里插入图片描述

下一座石碑

  • 这座石碑倒下了,露出了下一座石碑…

😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔
🚀专栏:C++爬塔日记
🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

封心锁爱的前夫哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值