set_intersection set_union set_difference set_symmetric_difference
`std::set_intersection` 用于计算两个已排序范围的交集。它将交集的结果写入到指定的输出迭代器中。
`std::set_union` 用于计算两个已排序范围的并集。它将并集的结果写入到指定的输出迭代器中。
`std::set_difference` 用于计算两个已排序范围的差集。它将差集的结果写入到指定的输出迭代器中。差集是指在第一个范围中存在但在第二个范围中不存在的元素。
`std::set_symmetric_difference` 用于计算两个已排序范围的对称差集。对称差集是指在两个范围中存在但不在两个范围的交集中存在的元素。它将对称差集的结果写入到指定的输出迭代器中。
即:并集减差集:set_union - set_intersection
这几个函数默认使用 < 运算符,除非自定义比较函数。
std::set_intersection
用于计算两个已排序范围的交集。它将交集的结果写入到指定的输出迭代器中。
| Defined in header | ||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_intersection( InputIt1 first1, InputIt1 last1, | (1) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > | (2) | (since C++17) |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare > | (3) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, | (4) | (since C++17) |
Constructs a sorted range beginning at d_first consisting of elements that are found in both sorted ranges [first1, last1) and [first2, last2).
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, the first std::min(m, n) elements will be copied from [first1, last1) to the output range, preserving order.
1) If [first1, last1) or [first2, last2) is not sorted with respect to operator<(until C++20)std::less{}(since C++20), the behavior is undefined.
3) If [first1, last1) or [first2, last2) is not sorted with respect to comp, the behavior is undefined.
2,4) Same as (1,3), but executed according to policy.
If the output range overlaps with [first1, last1) or [first2, last2), the behavior is undefined.
### 参数
- `first1`, `last1`: 第一个已排序范围的起始和结束迭代器。
- `first2`, `last2`: 第二个已排序范围的起始和结束迭代器。
- `d_first`: 输出范围的起始迭代器。
- `comp`: 二元谓词,用于比较元素。
### 返回值
返回一个迭代器,指向输出范围的末尾,即最后一个写入元素之后的位置。
### 示例
以下是一个简单的例子,演示如何使用 `std::set_intersection`:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>int main()
{std::vector<int> v1{7, 2, 3, 4, 5, 6, 7, 8};std::vector<int> v2{5, 7, 9, 7};std::sort(v1.begin(), v1.end());std::sort(v2.begin(), v2.end());std::vector<int> v_intersection;std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(v_intersection));for (int n : v_intersection)std::cout << n << ' ';std::cout << '\n';
}
Output:
5 7 7
### 自定义比较函数
#include <iostream>
#include <vector>
#include <algorithm>bool compare(int a, int b) {return a < b;
}int main() {std::vector<int> vec1 = {1, 2, 3, 4, 5};std::vector<int> vec2 = {3, 4, 5, 6, 7};std::vector<int> result;std::set_intersection(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result), compare);std::cout << "Intersection of vec1 and vec2: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
在这个例子中,`std::set_intersection` 使用 `compare` 函数来比较元素。
std::set_union
用于计算两个已排序范围的并集。它将并集的结果写入到指定的输出迭代器中。
### 语法
| Defined in header | ||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_union( InputIt1 first1, InputIt1 last1, | (1) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > | (2) | (since C++17) |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare > | (3) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, | (4) | (since C++17) |
Constructs a sorted union beginning at d_first consisting of the set of elements present in one or both sorted ranges [first1, last1) and [first2, last2).
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, then all m elements will be copied from [first1, last1) to the output range, preserving order, and then the final std::max(n - m, 0) elements will be copied from [first2, last2) to the output range, also preserving order.
1) If [first1, last1) or [first2, last2) is not sorted with respect to operator<(until C++20)std::less{}(since C++20), the behavior is undefined.
3) If [first1, last1) or [first2, last2) is not sorted with respect to comp, the behavior is undefined.
2,4) Same as (1,3), but executed according to policy.
These overloads participate in overload resolution only if all following conditions are satisfied:
If the output range overlaps with [first1, last1) or [first2, last2), the behavior is undefined.
### 参数
- `first1`, `last1`: 第一个已排序范围的起始和结束迭代器。
- `first2`, `last2`: 第二个已排序范围的起始和结束迭代器。
- `d_first`: 输出范围的起始迭代器。
- `comp`: 二元谓词,用于比较元素。
### 返回值
返回一个迭代器,指向输出范围的末尾,即最后一个写入元素之后的位置。
### 示例
以下是一个简单的例子,演示如何使用 `std::set_union`:
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec1 = {1, 2, 3, 4, 5};std::vector<int> vec2 = {3, 4, 5, 6, 7};std::vector<int> result;std::set_union(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result));std::cout << "Union of vec1 and vec2: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:
Union of vec1 and vec2: 1 2 3 4 5 6 7
### 自定义比较函数
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>struct MyStruct {int key;int value;
};// 自定义比较函数,比较 MyStruct 的 value 成员的绝对值
bool compare(const MyStruct& a, const MyStruct& b) {return std::abs(a.value) < std::abs(b.value);
}int main() {std::vector<MyStruct> vec1 = {{1, 10}, {2, -20}, {3, 30}, {4, -40}, {5, 50}};std::vector<MyStruct> vec2 = {{3, -300}, {4, 400}, {5, -500}, {6, 600}, {7, -700}};std::vector<MyStruct> result;std::set_union(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result), compare);std::cout << "Union of vec1 and vec2: ";for (const auto& elem : result) {std::cout << "{" << elem.key << ", " << elem.value << "} ";}std::cout << std::endl;return 0;
}
在这个例子中,`std::set_union` 使用 `compare` 函数来比较元素。
std::set_difference
用于计算两个已排序范围的差集。它将差集的结果写入到指定的输出迭代器中。差集是指在第一个范围中存在但在第二个范围中不存在的元素。
### 语法
| Defined in header | ||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_difference( InputIt1 first1, InputIt1 last1, | (1) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > | (2) | (since C++17) |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare > | (3) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, | (4) | (since C++17) |
Copies the elements from the sorted range [first1, last1) which are not found in the sorted range [first2, last2) to the range beginning at d_first. The output range is also sorted.
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, the final std::max(m - n, 0) elements will be copied from [first1, last1) to the output range, preserving order.
1) If [first1, last1) or [first2, last2) is not sorted with respect to operator<(until C++20)std::less{}(since C++20), the behavior is undefined.
3) If [first1, last1) or [first2, last2) is not sorted with respect to comp, the behavior is undefined.
2,4) Same as (1,3), but executed according to policy.
If the output range overlaps with [first1, last1) or [first2, last2), the behavior is undefined.
### 参数
- `first1`, `last1`: 第一个已排序范围的起始和结束迭代器。
- `first2`, `last2`: 第二个已排序范围的起始和结束迭代器。
- `d_first`: 输出范围的起始迭代器。
- `comp`: 二元谓词,用于比较元素。
### 返回值
返回一个迭代器,指向输出范围的末尾,即最后一个写入元素之后的位置。
### 示例
以下是一个简单的例子,演示如何使用 `std::set_difference`:
//在vec1中不在vec2中的元素
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> vec1 = {1, 3, 2, 4, 5};std::vector<int> vec2 = {3, 5, 4, 6, 7};std::vector<int> result;std::sort(vec1.begin(), vec1.end());std::sort(vec2.begin(), vec2.end());std::set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result));std::cout << "Difference of vec1 and vec2: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
输出:
Difference of vec1 and vec2: 1 2
### 自定义比较函数
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>struct MyStruct {int key;int value;
};// 自定义比较函数,比较 MyStruct 的 value 成员的绝对值
bool compare(const MyStruct& a, const MyStruct& b) {return std::abs(a.value) < std::abs(b.value);
}int main() {std::vector<MyStruct> vec1 = {{1, 10}, {2, -20}, {3, 30}, {4, -40}, {5, 50}, {9, -90}};std::vector<MyStruct> vec2 = {{3, -300}, {4, 400}, {5, -500}, {6, 600}, {7, -700}, {10, 1000}};std::vector<MyStruct> result;std::set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result), compare);std::cout << "Difference of vec1 and vec2: ";for (const auto& elem : result) {std::cout << "{" << elem.key << ", " << elem.value << "} ";}std::cout << std::endl;return 0;
}
在这个例子中,`std::set_difference` 使用 `compare` 函数来比较元素。
std::set_symmetric_difference
用于计算两个已排序范围的对称差集。对称差集是指在两个范围中存在但不在两个范围的交集中存在的元素。它将对称差集的结果写入到指定的输出迭代器中。
即:并集减差集
### 语法
| Defined in header | ||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_symmetric_difference | (1) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > | (2) | (since C++17) |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare > | (3) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
### 参数
- `first1`, `last1`: 第一个已排序范围的起始和结束迭代器。
- `first2`, `last2`: 第二个已排序范围的起始和结束迭代器。
- `d_first`: 输出范围的起始迭代器。
- `comp`: 二元谓词,用于比较元素。
### 返回值
返回一个迭代器,指向输出范围的末尾,即最后一个写入元素之后的位置。
### 示例
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>int main()
{std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8};std::vector<int> v2{5, 7, 9, 10};std::sort(v1.begin(), v1.end());std::sort(v2.begin(), v2.end());std::vector<int> v_symDifference;std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(v_symDifference));for (int n : v_symDifference)std::cout << n << ' ';std::cout << '\n';
}
Output:
1 2 3 4 6 8 9 10
### 自定义比较函数
#include <iostream>
#include <vector>
#include <algorithm>struct MyStruct {int key;int value;
};bool compare(const MyStruct& a, const MyStruct& b) {return a.key < b.key;
}int main() {std::vector<MyStruct> vec1 = {{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}};std::vector<MyStruct> vec2 = {{3, 300}, {4, 400}, {5, 500}, {6, 600}, {7, 700}};std::vector<MyStruct> result;std::set_symmetric_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(result), compare);std::cout << "Symmetric difference of vec1 and vec2: ";for (const auto& elem : result) {std::cout << "{" << elem.key << ", " << elem.value << "} ";}std::cout << std::endl;return 0;
}
output:
Symmetric difference of vec1 and vec2: {1, 10} {2, 20} {6, 600} {7, 700}
在这个例子中,`std::set_symmetric_difference` 使用 `compare` 函数来比较元素。
相关文章:
set_intersection set_union set_difference set_symmetric_difference
std::set_intersection 用于计算两个已排序范围的交集。它将交集的结果写入到指定的输出迭代器中。 std::set_union 用于计算两个已排序范围的并集。它将并集的结果写入到指定的输出迭代器中。 std::set_difference 用于计算两个已排序范围的差集。它将差集的结果写入到指…...
webpack打包优化策略
1. 减少打包体积 减少打包文件的大小是为了提高加载速度,降低网络带宽消耗,提升用户体验。常见的减少打包体积的优化策略包括: 代码分割(Code Splitting):将代码拆分成多个小文件,让浏览器按需…...
多线程基础面试题剖析
一、线程的创建方式有几种 创建线程的方式有两种,一种是继承Thread,一种是实现Runable 在这里推荐使用实现Runable接口,因为java是单继承的,一个类继承了Thread将无法继承其他的类,而java可以实现多个接口࿰…...
WEB安全--SQL注入--floor报错注入
一、原理: floor()报错注入需要组合count()、rand()、group by()等函数使用,通过一些手段使数据库在处理语句时产生主键重复的报错,从而达到爆出信息的目的 二、内容: ?id-1 or (select 1 from (select count(*),concat(databa…...
resultMap 标签
resultMap 是 MyBatis 框架中用于定义数据库结果集与 Java 对象之间映射关系的核心标签。它的主要作用是解决数据库字段名与 Java 对象属性名不一致的问题,或处理复杂查询(如关联查询、嵌套对象、集合映射等)时的映射需求。 主要用途&#x…...
17.推荐系统的在线学习与实时更新
接下来就讲解推荐系统的在线学习与实时更新。推荐系统的在线学习和实时更新是为了使推荐系统能够动态地适应用户行为的变化,保持推荐结果的实时性和相关性。以下是详细的介绍和实现方法。 推荐系统的在线学习与实时更新 在线学习的概念 在线学习(Onli…...
Android设备 网络安全检测
八、网络与安全机制 6.1 网络框架对比 volley: 功能 基于HttpUrlConnection;封装了UIL图片加载框架,支持图片加载;网络请求的排序、优先级处理缓存;多级别取消请求;Activity和生命周期的联动(Activity结束生命周期同时取消所有网络请求 …...
Kotlin 2.1.0 入门教程(二十)扩展
扩展 Kotlin 提供了一种能力,无需继承类或使用像装饰器这样的设计模式,就能为类或接口扩展新的功能。这是通过一种名为扩展的特殊声明来实现的。 例如,你可以为无法修改的第三方库中的类或接口编写新的函数。这些函数可以像原类的方法一样以…...
神经网络的学习 求梯度
import sys, ossys.path.append(os.pardir) import numpy as npfrom common.functions import softmax, cross_entropy_error from common.gradient import numerical_gradient# simpleNet类 class simpleNet:def __init__(self):self.W np.random.rand(2, 3) # 随机形状为2*…...
机器学习数学基础:24.随机事件与概率
一、教程目标 本教程致力于帮助零基础或基础薄弱的学习者,全面掌握概率论与数理统计的基础公式,透彻理解核心概念,熟练学会应用解题技巧,最终能够轻松应对期末或考研考试。 二、适用人群 特别适合那些对概率论与数理统计知识了…...
【NLP 24、模型训练方式】
你的痛苦,我都心疼,想为你解决 —— 25.2.15 一、按学习范式分类 1. 监督学习(Supervised Learning) 核心思想:使用带有标签(已知输入-输出对)的数据训练模型。 常见任务:分类&…...
【鸿蒙】ArkUI-X跨平台问题集锦
系列文章目录 【鸿蒙】ArkUI-X跨平台问题集锦 文章目录 系列文章目录问题集锦1、HSP,HAR模块中 无法引入import bridge from arkui-x.bridge;2、CustomDialog 自定义弹窗中的点击事件在Android 中无任何响应;3、调用 buildRouterMode() 路由跳转页面前,…...
AI向量数据库之LanceDB快速介绍
LanceDB LanceDB 是一个开源的向量搜索数据库,具备持久化存储功能,极大地简化了嵌入向量的检索、过滤和管理。 LanceDB的主要特点 LanceDB 的主要特点包括: 生产级向量搜索:无需管理服务器。 存储、查询和过滤向量、元数据以…...
嵌入式玩具--无人机字幕
day01 01-无人机-组成结构-上 哎,好,各位,那现在呢我们一起来看一下,就是咱们接下来要做的这个小项目啊。呃,当然这个名字有很多啊,就是这种飞行器有管,它叫四旋翼飞行器的,也有叫…...
CentOS7 安装配置FTP服务
CentOS7 安装配置FTP服务 CentOS7 安装配置FTP服务1. FTP简介2. 先行准备2.1 关闭防火墙2.2 关闭 SELinux 3.安装FTP软件包4. 创建 FTP 用户及目录4.1 创建 FTP 目录并设置权限4.2 防止 FTP 用户登录 Linux 终端4.3 创建 FTP 用户组及用户4.4 创建 FTP 可写目录 5. 配置ftp服务…...
几款dxf文件转Gcode的开源软件
以下是一些常用的开源软件,可以将DXF文件转换为Gcode: 1. **Inkscape with Gcode Tools** - **Inkscape** 是一款开源的矢量图形编辑器,支持DXF文件导入。通过安装 **Gcode Tools** 插件,可以将矢量图形转换为Gcode。 - 官网: [Inkscape](https://inkscape.org/) …...
【设计模式】03-理解常见设计模式-行为型模式(专栏完结)
前言 前面我们介绍完创建型模式和创建型模式,这篇介绍最后的行为型模式,也是【设计模式】专栏的最后一篇。 一、概述 行为型模式主要用于处理对象之间的交互和职责分配,以实现更灵活的行为和更好的协作。 二、常见的行为型模式 1、观察者模…...
【计算机网络】传输层数据段格式
在计算机网络中,数据段(Segment) 是传输层协议(如 TCP 或 UDP)使用的数据单元。TCP 和 UDP 的数据段格式有所不同,以下是它们的详细说明: 1. TCP 数据段格式 TCP(传输控制协议&…...
编程题-最大子数组和(中等-重点【贪心、动态规划、分治思想的应用】)
题目: 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 子数组是数组中的一个连续部分。 解法一(枚举法-时间复杂度超限): …...
网络将内网服务转换到公网上
当然,以下是根据您提供的描述,对内网端口在公网上转换过程的详细步骤,并附上具体例子进行说明: 内网端口在公网上的转换过程详细步骤 1. 内网服务配置 步骤说明: 在内网中的某台计算机(我们称之为“内网…...
本地通过隧道连接服务器的mysql
前言 服务器上部署了 mysql,本地希望能访问该 mysql,但是又不希望 mysql 直接暴露在公网上 那么可以通过隧道连接 ssh 端口的方式进行连接 从外网看,服务器只开放了一个 ssh 端口,并没有开放 3306 监听端口 设置本地免密登录 …...
跳跃游戏 II - 贪心算法解法
问题描述: 给定一个长度为 n 的 0 索引整数数组 nums,我们从数组的第一个元素 nums[0] 开始。每个元素 nums[i] 表示从索引 i 可以跳跃的最大长度,换句话说,从位置 i,你可以跳到位置 i j,其中 0 < j &…...
2. grafana插件安装并接入zabbix
一、在线安装 如果不指定安装位置,则默认安装位置为/var/lib/grafana/plugins 插件安装完成之后需要重启grafana 命令在上一篇讲到过 //查看相关帮助 [rootlocalhost ~]# grafana-cli plugins --help //从列举中的插件过滤zabbix插件 [rootlocalhost ~]# grafana…...
Linux第107步_Linux之PCF8563实验
使用PCF8563代替内核的RTC,可以降低功耗,提高时间的精度。同时有助于进一步熟悉I2C驱动的编写。 1、了解rtc_time64_to_tm()和rtc_tm_to_time64() 打开“drivers/rtc/lib.c” /* * rtc_time64_to_tm - Converts time64_t to rtc_time. * Convert seco…...
功能说明并准备静态结构
功能说明并准备静态结构 <template><div class"card-container"><!-- 搜索区域 --><div class"search-container"><span class"search-label">车牌号码:</span><el-input clearable placeho…...
pip 与 conda 的故事
pip 换源 pip 官方源 -i https://pypi.python.org/simple pip 清华源 -i https://pypi.tuna.tsinghua.edu.cn/simple pip 阿里源 -i https://mirrors.aliyun.com/pypi/simple PyTorch 安装 pip3 install torch torchvision torchaudio pip3 install torch torchvision torchaud…...
【05】RUST错误处理
文章目录 错误处理panic代码运行 ResutResult中的一些方法介绍传播错误?运算符 错误处理 建议是尽量用Result由调用者自行决定是否恢复,不恢复也可直接在Err中调用panic。代码分支不可能走的分支可panic。 需要panic的情况: 有害状态&#x…...
[免费]SpringBoot公益众筹爱心捐赠系统【论文+源码+SQL脚本】
大家好,我是老师,看到一个不错的SpringBoot公益众筹爱心捐赠系统,分享下哈。 项目介绍 公益捐助平台的发展背景可以追溯到几十年前,当时人们已经开始通过各种渠道进行公益捐助。随着互联网的普及,本文旨在探讨公益事业…...
算法【动态规划中使用观察优化枚举】
动态规划的问题中,已经写出了记忆化搜索的版本,还要写出严格位置依赖的版本,意义在于不仅可以进行空间压缩优化;关键还在于,很多时候通过进一步观察,可以优化枚举,让时间复杂度更好。优化枚举的…...
ML.Net二元分类
ML.Net二元分类 文章目录 ML.Net二元分类前言项目的创建机器学习模型的创建添加模型选择方案训练环境的选择训练数据的添加训练数据的选择训练数据的格式要预测列的选择模型评估模型的使用总结前言 ML.NET是由Microsoft为.NET开发者平台创建的免费、开源、跨平台的机器学习…...
