当前位置: 首页 > news >正文

C++ STL 之常用拷贝和替换算法①copy();②replace();③replace_if();④swap();

VS2017 程序下载:https://pan.baidu.com/s/1ZTFWrl4rG8Z1PxL7vzrOdw?pwd=i7pt

目录

1.copy

2.replace

3.replace_if

4.swap


1.copy

功能描述:

容器内指定范围的元素拷贝到另一容器中

函数原型:

// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器 end 结束迭代器 dest 目标起始迭代器
copy(iterator beg, iterator end, iterator dest); 

程序:

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

//普通函数打印数据
void myPrint(int val)
{
	cout << val << " ";
}

//仿函数 打印数据
class print02
{
public:
	void operator()(int val)//重载()
	{
		cout << val << " ";
	}
};

void printVector(vector<int>&v)//遍历打印
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << " ---print end--- " << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);//尾插数据 0~9
	}
	cout << " v1 数据: ";
	printVector(v1);

	vector<int>v2;
	v2.resize(v1.size());//开辟空间

	//将v1所有元素拷贝到v2
	copy(v1.begin(), v1.end(), v2.begin());
	cout <<  " 调用普通函数遍历,打印v2数据: ";
	//遍历元素,并打印
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;

	cout << " 调用仿函数遍历,打印v2数据: ";
	for_each(v2.begin(), v2.end(), print02());
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

2.replace

功能描述:

将容器内指定范围的旧元素修改为新元素

函数原型:

// 将区间内旧元素 替换成 新元素
// beg 开始迭代器 end 结束迭代器 oldvalue 旧元素 newvalue 新元素
replace(iterator beg, iterator end, oldvalue, newvalue); 

程序:

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

//普通函数打印数据
void myPrint(int val)
{
	cout << val << " ";
}

//仿函数 打印数据
class print02
{
public:
	void operator()(int val)//重载()
	{
		cout << val << " ";
	}
};

void printVector(vector<int>&v)//遍历打印
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << " ---print end--- " << endl;
}

void test01()
{

	vector<int>v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(10);
	v.push_back(20);
	cout << " v 数据: ";
	printVector(v);

	cout << " 调用普通函数遍历打印 替换前 v数据: ";
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//将20 替换 88
	replace(v.begin(), v.end(), 20, 88);
	cout << " 调用 仿函数遍历打印 替换后 v 数据: ";
	for_each(v.begin(), v.end(), print02());
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

3.replace_if

功能描述:

将区间内满足条件的元素,替换成指定元素

函数原型:

// 按条件替换元素,满足条件的替换成指定元素
// beg 开始迭代器, end 结束迭代器, _pred 谓词, newvalue 替换的新元素
replace_if(iterator beg, iterator end, _pred, newvalue); 

程序:

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

//普通函数打印数据
void myPrint(int val)
{
	cout << val << " ";
}

//仿函数 打印数据
class MyPrint
{
public:
	void operator()(int val)//重载()
	{	
		cout << val << " ";
	}
};

class Greater30
{
public:
	bool operator()(int val)//判断数据是否 ≥30
	{
		return val >= 30;
	}
};

void printVector(vector<int>&v)//遍历打印
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << " ---print end--- " << endl;
}

//常用拷贝和替换算法 replace_if
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(30);
	cout << " v 数据: ";
	printVector(v);

	cout << "替换前: " << endl;
	cout << " 调用普通函数遍历打印 替换前 v数据: ";
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//将大于等于30的数 全部 替换为 88
	replace_if(v.begin(), v.end(), Greater30(),88);
	cout << "替换后: " << endl;
	cout << " 调用 仿函数遍历打印 替换前 v 数据: ";
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

4.swap

功能描述:

互换两个容器的元素

函数原型:


// 互换两个容器的元素
// c1:容器1, c2:容器2
swap(container c1, container c2); 

程序:

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

//普通函数 打印数据
void myPrint(int val)
{
	cout << val << " ";
}

//仿函数 打印数据
class MyPrint
{
public:
	void operator()(int val)//重载()
	{	
		cout << val << " ";
	}
};

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+10);
	}

	cout << "交换前 v1: " << endl;
	//遍历 打印数据
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	cout << "交换前 v2: " << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;

	cout << "-----------------" << endl;
	
	swap(v1, v2);//v1 与 v2 交换数据
	cout << "交换后 v1: " << endl;
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	cout << "交换后 v2: " << endl;
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

相关文章:

  • 【操作系统】Ch5 存储管理
  • 行为模式---访问者模式
  • 实战:自适应均衡的设计与实现
  • SOC与电压的关系
  • 谈谈你对前端工程化的理解,它包含哪些方面
  • Linux目录理解
  • MySQL-基础篇
  • docker 安装mysql
  • 网络安全 --- 基于网络安全的 Linux 最敏感目录及文件利用指南
  • 有趣的算法实践:整数反转与回文检测(Java实现)
  • 数据挖掘导论——第七章:聚类
  • 如何通过用户分群优化应用用户体验
  • OceanBase 用户问题精选答疑:OceanBase 版本升级解析
  • 如何在手机上绘制CAD虚线?
  • 【Halcon】灰度不均解决方案
  • SpringBoot自定义线程池
  • 解决 React Native 0.76 中 com.facebook.react.settings 插件缺失问题
  • Ubuntu docker安装milvusdb
  • Matlab 四分之一车辆被动悬架和模糊pid控制对比
  • 网页制作17-Javascipt图像特效の鼠标经过图像震动
  • 陈吉宁龚正黄莉新胡文容等在警示教育基地参观学习,出席深入贯彻中央八项规定精神学习教育交流会
  • 美政府以拨款为要挟胁迫各州服从移民政策,20个州联合起诉
  • 经济日报:美国滥施汽车关税损人不利己
  • 中美是否计划讨论美方以芬太尼为由对华征收的特别关税?外交部回应
  • 演员黄晓明、金世佳进入上海戏剧学院2025年博士研究生复试名单
  • 铁路部门:确保沿线群众安全,焦柳铁路6个区段将陆续安装防护栅栏