STL——常用算法(二)

news2024/10/6 10:29:48

一、常用拷贝和替换算法

1.copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{
	cout << val << " ";
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	vector<int>v2;
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), printVector);
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

2.replace

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{
	cout << val << " ";
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	replace(v1.begin(), v1.end(), 5, 10);
	for_each(v1.begin(), v1.end(), printVector);
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

3.replace_if

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
class Greater5
{
public:
	bool operator()(int val)
	{
		return val >= 5;
	}
};
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;

	replace_if(v1.begin(), v1.end(), Greater5(), 10);
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

4.swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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 + 100);
	}
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;

	swap(v1, v2);
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

二、常用算数生成算法

1.accumulate

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i <= 100; i++)
	{
		v1.push_back(i);
	}
	int total = accumulate(v1.begin(), v1.end(), 1000);//5050+1000
	//for_each(v1.begin(), v1.end(), MyPrint());
	cout << total << endl;
}
int main()
{
	test01();
	return 0;
}

2.fill

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int>v1;
	v1.resize(10);
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	fill(v1.begin(), v1.end(), 10);
	for_each(v1.begin(), v1.end(), MyPrint());
}
int main()
{
	test01();
	return 0;
}

三、常用集合算法

1.set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);
	}
	vector<int>v3;
	v3.resize(min(v1.size(), v2.size()));
	vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), itEnd, MyPrint);
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

2.set_union

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);
	}
	vector<int>v3;
	v3.resize(v1.size() + v2.size());
	vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), itEnd, MyPrint);
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

3.set_difference

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 + 5);
	}
	vector<int>v3;
	v3.resize(max(v1.size(), v2.size()));
	vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), itEnd, MyPrint);
	cout << endl;
	vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
	for_each(v3.begin(), itEnd1, MyPrint);
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1862594.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

监控https证书的到期时间

实现该功能&#xff0c;不用借助第三方库&#xff0c;用go的标准库就足够了… 以下程序可以获取这些域名的SSL证书的到期时间&#xff0c;并在证书距离现在不足7天过期时打印提示&#xff1a; package mainimport ("crypto/tls""fmt""net""…

酒店强心剂——VR智慧酒店上线,史诗级加强入住率

出门在外&#xff0c;什么才是我们最为头疼的问题呢&#xff1f;衣食住行中&#xff0c;住的问题尤其大&#xff0c;尤其是不熟悉当地情况下&#xff0c;预定酒店才是让人头疼的问题。酒店行业该如何化解这一难题呢&#xff1f;VR全景开启智能化酒店宣传获客新模式&#xff0c;…

Hive笔记-6

6.2.8 聚合函数 1) 语法 count(*)&#xff0c;表示统计所有行数&#xff0c;包含null值&#xff1b; count(某列)&#xff0c;表示该列一共有多少行&#xff0c;不包含null值&#xff1b; max()&#xff0c;求最大值&#xff0c;不包含null&#xff0c;除非所有值都是null&a…

高考填报志愿攻略,5个步骤选专业和院校

在高考完毕出成绩的时候&#xff0c;很多人会陷入迷茫中&#xff0c;好像努力了这么多年&#xff0c;却不知道怎么规划好未来。怎么填报志愿合适&#xff1f;在填报志愿方面有几个内容需要弄清楚&#xff0c;按部就班就能找到方向&#xff0c;一起来了解一下正确的步骤吧。 第…

Docker中修改TiDB数据库密码(类似mysql)

1.Docker容器运行TiDB pingcap/tidb:last 2.登陆容器系统&#xff1a; 3.在容器中安装mysql客户端&#xff1a; 4.空密码登陆TiDB 5.修改TiDB密码并退出 6.使用修改后的密码登陆验证&#xff1a;

福州代理记账服务财务专业知识会计助手

福州的代理记服务可探索企业和个体工商户处理财务和会计工作。选择合适的代理记服务不仅可以节省成本&#xff0c;还可以确保财务工作专业、合规。以下是一些关于代理记服务的关键信息和财务信息&#xff0c;供您参考&#xff1a; https://www.9733.cn/news/detail/180.html …

TensorFlow开源项目

欢迎来到 Papicatch的博客 文章目录 &#x1f349;TensorFlow介绍 &#x1f349;主要特点和功能 &#x1f348;多语言支持 &#x1f348;灵活的架构 &#x1f348;分布式训练 &#x1f348;跨平台部署 &#x1f348;强大的工具链 &#x1f348;丰富的社区和生态系统 &a…

缓冲池管理器

开发环境搭建 克隆 git clone https://github.com/cmu-db/bustub.git cd bustub/ 切换分支 git checkout -b branchname v20221128-2022fall 创建docker镜像 docker build . -t bustub_img 创建容器 docker create -it --name bustub_container -v “E:/cmu/bustub”:“/bustu…

JeecgFlow事件网关概念及案例

事件网关 通常网关基于连线条件决定后续路径&#xff0c;但事件网关有所不同&#xff0c;其基于事件决定后续路径。事件网关的每条外出顺序流都需要连接一个捕获中间事件。 事件网关只有分支行为&#xff0c;流程的走向完全由中间事件决定。可以从多条候选分支中选择事件最先达…

Core ML 简介:构建简单的图像识别应用程序

在 2017 年的 WWDC 上&#xff0c;苹果发布了许多令人兴奋的框架和 API 供我们开发人员使用。在所有新框架中&#xff0c;最受欢迎的框架之一肯定是Core ML。Core ML 是一个可用于将机器学习模型集成到您的应用程序中的框架。Core ML 最好的部分是您不需要有关神经网络或机器学…

计算机组成原理 | CPU子系统(2)指令系统

CISC和RISC指令集 指令的一般格式 四种结构 R型&#xff1a;寄存器型&#xff08;四地址&#xff09; I型&#xff1a;立即数型&#xff08;三地址&#xff09; J型&#xff1a;跳转型&#xff08;address以立即数的形式给出&#xff09; 格式规整&#xff0c;高六位都是操作…

昇思25天学习打卡营第01天|基本介绍

作为曾经的javaer&#xff0c;本着不断学习的初心&#xff0c;报名了昇思25天的课程&#xff0c;希望自己能学会点东西的目的。 昇思MindSpore介绍 昇思MindSpore是一个全场景深度学习框架&#xff0c;旨在实现易开发、高效执行、全场景统一部署三大目标。 其中&#xff0c;…

解决chrome浏览器总是将对站点的http访问改为https的问题

问题&#xff1a;vue项目本地运行出来的地址是http开头的&#xff0c;但在chrome浏览器中无法访问&#xff0c;在Edge浏览器就可以&#xff0c;发现是chrome总是自动将http协议升级为https。 已试过的有效的方法&#xff1a; 一、无痕模式下访问 无痕模式下访问不会将http自…

《中国储运》杂志社中国储运杂志社中国储运编辑部2024年第6期目录

卷首语 提升物流质效 助力经济发展 楚耘; 12 专栏 大力发展新质生产力 依托新模式新业态推动物流成本有效降低 房永斌; 16 访谈 中国国际发展知识中心副主任、国务院发展研究中心研究员魏际刚&#xff1a;对促进物流行业降本增效的十个建议 李静宇; 17-19 特别策划…

【摄像头标定】使用kalibr进行双目摄像头标定(ros1、ros2)

使用kalibr进行双目摄像头标定 前言标定板标定①板端准备和录制②上位机准备和标定 前言 本文不是纯用ros1进行标定&#xff0c;需要ros1和ros2通信。给使用ros2进行开发&#xff0c;但又想用kalibr标定双目摄像头的小伙伴一个教程。本文双目摄像头的数据发布使用ros2&#xf…

MySQL中的Bin-log是什么?有什么作用?

Bin-log日志也被称之为二进制日志&#xff0c;作用与Redo-log类似&#xff0c;主要是记录所有对数据库表结构变更和表数据修改的操作&#xff0c;对于select、show这类读操作并不会记录。bin-log是MySQL-Server级别的日志&#xff0c;所有引擎都能用的日志&#xff0c;而redo-l…

LKD-Net: Large Kernel Convolution Network for Single Image Dehazing

LKD-Net&#xff1a;用于单幅图像去噪的大型核卷积网络 摘要 基于深度卷积神经网络(CNN)的单幅图像去噪方法已经取得了很大的成功。以往的方法致力于通过增加网络的深度和宽度来提高网络的性能。目前的方法侧重于增加卷积核的大小&#xff0c;以受益于更大的接受野来增强其性能…

【自然语言处理系列】探索NLP:使用Spacy进行分词、分句、词性标注和命名实体识别,并以《傲慢与偏见》与全球恐怖活动两个实例文本进行分析

本文深入探讨了scaPy库在文本分析和数据可视化方面的应用。首先&#xff0c;我们通过简单的文本处理任务&#xff0c;如分词和分句&#xff0c;来展示scaPy的基本功能。接着&#xff0c;我们利用scaPy的命名实体识别和词性标注功能&#xff0c;分析了Jane Austen的经典小说《傲…

LabVIEW技术交流-控件的禁用属性与Mouse Up事件的一个坑

问题来源 我们平时对控件Mouse Up事件触发使用场景不多&#xff0c;可能在按钮控件上会偶尔用到。在一些场景中&#xff0c;我们用按钮的Mouse Up触发事件&#xff0c;但是又希望在某些限制条件下&#xff0c;按钮会被禁用而不能触发事件。 可是当我们禁用按钮时&#xff0c;它…

网页设计的意义何在?最后一个你绝对没想到!

在当今时代&#xff0c;网页已经成为我们日常生活中不可或缺的一部分。网页的支持对于搜索信息、购物、社交娱乐、在线学习和工作至关重要。网页设计作为网页的重要组成部分之一&#xff0c;在实现网页的各种功能和目的方面发挥着至关重要的作用。那么&#xff0c;网页设计的目…