常用集合算法
学习目标:
- 掌握常用的集合算法
算法简介:
-
set_intersection
// 求两个容器的交集 -
set_union
// 求两个容器的并集 -
set_difference
// 求两个容器的差集
1. set_intersection
功能描述:
- 求两个容器的交集
函数原型:
-
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的交集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
例1:v1升序,v2升序
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*
**功能描述:**
* 求两个容器的交集
**函数原型:**
- `set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的交集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
v1.push_back(15);
cout << "v1排序后(升序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容器升序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end());
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
//目标容器开辟空间需要从两个容器中取小值
// min函数需要包含头文件#include<algorithm>
v3.resize(min(v1.size(),v2.size()));
cout << "v3.size() =v1.size()=" << v3.size() << endl << endl;
cout << "把v1和v2交集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_intersection返回值是交集中最后一个元素的位置" << endl;
for_each(v3.begin(), itEnd, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
结果正确
例2:v1升序,v2降序
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*
**功能描述:**
* 求两个容器的交集
**函数原型:**
- `set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的交集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
v1.push_back(15);
cout << "v1排序后(升序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容器降序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end(),Mycompare1);
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
v3.resize(v1.size());
cout << "v3.size() =v1.size()=" << v3.size() << endl << endl;
cout << "把v1和v2交集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator itEnd= set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_intersection返回值是交集中最后一个元素的位置" << endl;
for_each(v3.begin(), itEnd, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
报错!!
例3:v1降序,v2升序
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*
**功能描述:**
* 求两个容器的交集
**函数原型:**
- `set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的交集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
v1.push_back(15);
cout << "v1排序后(降序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end(),Mycompare1);
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容器升序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end());
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
v3.resize(v1.size());
cout << "v3.size() =v1.size()=" << v3.size() << endl << endl;
cout << "把v1和v2交集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator itEnd= set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_intersection返回值是交集中最后一个元素的位置" << endl;
for_each(v3.begin(), itEnd, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
报错!!!!
例4:v1降序,v2降序
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*
**功能描述:**
* 求两个容器的交集
**函数原型:**
- `set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的交集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
v1.push_back(15);
cout << "v1排序后(降序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end(),Mycompare1);
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容器降序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end(),Mycompare1);
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
v3.resize(v1.size());
cout << "v3.size() =v1.size()=" << v3.size() << endl << endl;
cout << "把v1和v2交集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator itEnd= set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_intersection返回值是交集中最后一个元素的位置" << endl;
for_each(v3.begin(), itEnd, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
报错!!!
总结:
- 求交集的两个集合必须是有序序列。
- 两者排序规则必须一致,且都是升序
- 目标容器开辟空间需要从两个容器中取小值
- set_intersection返回值是交集中最后一个元素的位置
- 由于所学知识有限,暂时不知道set_intersection算法针对自定义数据类型该怎么写
2. set_union
功能描述:
- 求两个集合的并集,结果自动去重
- 也就是将两个int[](必须有序)合并到一个vector中,结果自动去重
- 两个重复元素相并,编译器会去掉一个重复元素
函数原型:
-
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的并集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*`set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的并集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
cout << "v1排序后(升序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容升序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end());
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
//目标容器开辟空间需要两个容器相加
v3.resize(v1.size()+v2.size());
cout << "v3.size() =" << v3.size() << endl << endl;
cout << "把v1和v2并集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator intEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_union返回值是并集中最后一个元素的位置" << endl;
for_each(v3.begin(), intEnd, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
总结:
- 求并集的两个集合必须的有序序列
- 两者排序规则必须一致,且都是升序
- 目标容器开辟空间需要两个容器相加
- set_union返回值是并集中最后一个元素的位置
3. set_difference,特别注意要指明对象。
功能描述:
- 求两个集合的差集,也就是非交集的部分。不过要分清对象
函数原型:
-
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的差集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
/*
`set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); `
// 求两个集合的差集
// **注意:两个集合必须是有序序列**
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
*/
void Myprint1(int n) {
cout << n << " ";
}
bool Mycompare1(int a, int b) {
return a > b;
}
void test01()
{
cout << "针对内置数据类型" << endl;
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
v1.push_back(66);
v1.push_back(6);
cout << "v1排序后(升序),v1里的元素如下:" << endl;
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Myprint1); cout << endl << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v2.push_back(66);
v2.push_back(48);
v2.push_back(39);
v2.push_back(8);
v2.push_back(10);
v2.push_back(15);
cout << "v2容升序排序后,里面的元素如下:" << endl;
sort(v2.begin(), v2.end());
for_each(v2.begin(), v2.end(), Myprint1); cout << endl << endl;
vector<int> v3;
//取两个里面较大的值给目标容器开辟空间
// max函数需要包含头文件#include<algorithhm>
v3.resize(max(v1.size(), v2.size()));
cout << "v3.size() =max(v1.size(), v2.size())=" << v3.size() << endl << endl;
cout << "把v1和v2差集放到v3里,v3里的内容如下:" << endl;
vector<int>::iterator intEnd1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint1); cout << endl << endl;
cout << "set_difference返回值既是差集中最后一个元素的位置" << endl;
cout << "v1和v2的差集如下:" << endl;
for_each(v3.begin(), intEnd1, Myprint1); cout << endl << endl;
vector<int> v4;
//取两个里面较大的值给目标容器开辟空间
// max函数需要包含头文件#include<algorithhm>
v4.resize(max(v1.size(), v2.size()));
cout << "v4.size() =max(v1.size(), v2.size())=" << v4.size() << endl << endl;
cout << "把v2和v1差集放到v4里,v4里的内容如下:" << endl;
vector<int>::iterator intEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v4.begin());
for_each(v4.begin(), v4.end(), Myprint1); cout << endl << endl;
cout << "set_difference返回值既是差集中最后一个元素的位置" << endl;
cout << "v2和v1的差集如下:" << endl;
for_each(v4.begin(), intEnd2, Myprint1); cout << endl << endl;
}
int main() {
test01();
cout << endl << endl << endl;
system("pause");
return 0;
}
总结:
-
求差集的两个集合必须的有序序列
-
两者排序规则必须一致,且都是升序
-
差集要注意指明对象,是谁和谁的差集
-
目标容器开辟空间需要从两个容器取较大值
-
set_difference返回值是差集中最后一个元素的位置