概述:
- 算法主要是由头文件
<algorithm>
<functional>
<numeric>
组成 <algorithm>
是所有STL头文件中最大的一个,范围涉及比较、 交换、查找、遍历操作、复制、修改等等<numeric>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数<functional>
定义了一些模板类,用以声明函数对象。
5.1. 常用遍历算法
学习目标:
- 掌握常用的遍历算法
算法简介:
for_each
//遍历容器transform
//搬运容器到另一个容器中
5.1.1. for_each
功能描述:
- 实现遍历容器
- 在实际开发中最常用的遍历算法,必须熟练掌握
函数原型:
for_each(iterator beg, iterator end, _func);
// 遍历算法 遍历容器元素
// beg 开始迭代器
// end 结束迭代器
// _func 函数或者函数对象
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
// 普通函数
void Print01(int val)
{
cout << val << ' ';
}
// 仿函数
class Print02
{
public:
void operator()(int val)
{
cout << val << ' ';
}
};
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), Print01);
cout << endl;
for_each(v.begin(), v.end(), Print02());
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
5.1.2. transform
功能描述:
- 搬运容器到另一个容器中
函数原型:
transform(iterator begl, iterator endl, iterator beg2, _func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
// 仿函数
class Transform
{
public:
int operator()(int val)
{
return val + 100;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << ' ';
}
};
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int> vTarget;
vTarget.resize(v.size()); // 目标容器需要提前开辟空间
transform(v.begin(), v.end(), vTarget.begin(), Transform());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
100 101 102 103 104 105 106 107 108 109
5.2. 常用查找算法
学习目标:
- 掌握常用的查找算法
算法简介:
find
//查找元素find_if
//按条件查找元素adjacent_find
//查找相邻重复元素binary_search
//二分查找法count
//统计元素个数count_if
//按条件统计元素个数
5.2.1. find
功能描述:
- 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
- 利用find可以在容器中找到指定的元素,返回值是迭代器
函数原型:
find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = -5; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "not find!" << endl;
}
else
{
cout << "find-" << *it << endl;
}
}
// 查找自定义数据类型
class Person
{
public:
Person(string name, int age) : m_name(name), m_age(age){}
// 重载 == ,让底层的find知道如何对比person数据类型
bool operator==(const Person & p)
{
if(this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void Test02()
{
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find(v.begin(), v.end(), p2);
if (it == v.end())
{
cout << "not find!" << endl;
}
else
{
cout << "find-name:" << it->m_name << " find-age:" << it->m_age << endl;
}
}
int main()
{
Test01();
Test02();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
find-5
find-name:bbb find-age:20
5.2.2. find_if
功能描述:
- 按条件查找元素
函数原型:
find _if(iterator beg, iterator end, _pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
class Person
{
public:
Person(string name, int age) : m_name(name), m_age(age){}
string m_name;
int m_age;
};
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if(it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了大于5的数字为:" << *it << endl;
}
}
class Greater20
{
public:
bool operator()(Person &p)
{
return p.m_age > 20;
}
};
void Test02()
{
vector<Person> v;
Person p1("孙悟空", 100);
Person p2("猪八戒", 80);
Person p3("沙僧", 30);
Person p4("唐僧", 10);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
if(it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了大于20岁的人:" << it->m_name << " " << it->m_age << endl;
}
}
int main()
{
Test01();
Test02();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
找到了大于5的数字为:6
找到了大于20岁的人:孙悟空 100
5.2.3. adjacent_find
功能描述:
- 查找相邻重复元素
函数原型:
adjacent find(iterator beg, iterator end);
// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
// beg 开始迭代器
// end 结束迭代器
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
// 查找内置数据类型
void Test01()
{
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(4);
v.push_back(6);
v.push_back(6);
v.push_back(8);
v.push_back(10);
v.push_back(10);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if(it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到的第一个相邻重复元素为:" << *it << endl;
}
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
找到的相邻重复元素为:6
5.2.4. binary_search
功能描述:
- 查找指定元素是否存在
- 二分查找,指数级运算,速度非常快
- 二分查找法查找效率很高,值得注意的是查找的容器中的元素必须是有序序列,否则结果不一定对
函数原型:
bool binary search(iterator beg, iterator end, value);
// 查找指定的元素,查到 返回true 否则false
// 注意:在无序序列中不可用
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
// 查找内置数据类型
void Test01()
{
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(4);
v.push_back(6);
v.push_back(6);
v.push_back(8);
v.push_back(10);
v.push_back(10);
bool ret = binary_search(v.begin(), v.end(), 9);
if(!ret)
{
cout << "没有找到" << endl;
}
else
{
cout << "成功找到" << endl;
}
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
没有找到
5.2.5. count
功能描述:
- 统计元素个数
函数原型:
count(iterator beg, iterator end, value);
// 统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// value 统计的元素
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
class Person
{
public:
Person(string name, int age) : m_name(name), m_age(age){}
bool operator==(const Person & p)
{
if(this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
// 查找内置数据类型
void Test01()
{
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(4);
v.push_back(6);
v.push_back(6);
v.push_back(8);
v.push_back(10);
v.push_back(10);
v.push_back(10);
int num = count(v.begin(), v.end(), 10);
cout << "10的元素个数为:" << num << endl;
}
void Test02()
{
vector<Person> v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 30);
Person p5("曹操", 44);
Person p("诸葛亮", 35);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count(v.begin(), v.end(), p);
cout << "和诸葛亮同岁的人有:" << num << "个" << endl;
}
int main()
{
Test01();
Test02();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
10的元素个数为:3
和诸葛亮同岁的人有:3个
5.2.6. count_if
功能描述:
- 按条件统计元素个数
函数原型:
count_if(iterator beg, iterator end, _Pred);
// 按条件统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
class Person
{
public:
Person(string name, int age) : m_name(name), m_age(age){}
bool operator==(const Person & p)
{
if(this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
class Greater20
{
public:
bool operator()(int v1)
{
return v1 > 20;
}
};
// 查找内置数据类型
void Test01()
{
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(4);
v.push_back(6);
v.push_back(6);
v.push_back(8);
v.push_back(10);
v.push_back(30);
v.push_back(50);
int num = count_if(v.begin(), v.end(), Greater20());
cout << "大于20的元素个数为:" << num << endl;
}
class GreaterPerson
{
public:
bool operator()(const Person & p)
{
return p.m_age > 20;
}
};
void Test02()
{
vector<Person> v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 30);
Person p5("曹操", 44);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count_if(v.begin(), v.end(), GreaterPerson());
cout << "年龄大于20的人数:" << num << endl;
}
int main()
{
Test01();
Test02();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
大于20的元素个数为:2
年龄大于20的人数:5
5.3. 常用排序算法
学习目标:
- 掌握常用的排序算法
算法简介:
sort
// 对容器内元素进行排序random_shuffle
// 洗牌,指定范围内的元素随机调整次序merge
// 容器元素合并,并存储到另一容器中reverse
// 反转指定范围的元素
5.3.1. sort
功能描述:
- 对容器内元素进行排序(默认从小到大)
函数原型:
sort(iterator beg, iterator end, _Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
// 查找内置数据类型
void Test01()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(2);
v.push_back(4);
v.push_back(9);
v.push_back(7);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
1 2 3 4 5 7 9
9 7 5 4 3 2 1
5.3.2. random_shuffle
功能描述:
- 洗牌,指定范围内的元素随机调整次序
- 若想每次都不一样,要加随机数种子
函数原型:
random_shuffle(iterator_beg, iterator_end);
// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<ctime>
void MyPrint(int val)
{
cout << val << " ";
}
void Test01()
{
srand((unsigned int) time (NULL));
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 8 6 4 5 1 7 2 3 9
0 3 1 2 4 6 8 9 7 5
5.3.3. merge
功能描述:
- 两个容器元素合并,并存储到另一容器中
- 目标容器需要提前开辟空间
- 合并后的序列还是一个有序序列
函数原型:
merge(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
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+1);
}
vector<int> v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), MyPrint);
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
5.3.4. reverse
功能描述:
- 将容器内元素进行反转
函数原型:
reverse(iterator beg,iterator end);
// 反转指定范围的元素
// beg 开始迭代器
// end 结束迭代器
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
9 8 7 6 5 4 3 2 1 0
5.4. 常用拷贝和替换算法
学习目标:
- 掌握常用的拷贝和替换算法
算法简介:
copy
// 容器内指定范围的元素拷贝到另一容器中replace
// 将容器内指定范围的旧元素修改为新元素replace_if
// 容器内指定范围满足条件的元素替换为新元素swap
// 互换两个容器的元素
5.4.1. copy
功能描述:
- 容器内指定范围的元素拷贝到另一容器中
- 目标容器要提前开辟空间
函数原型:
copy(iterator beg,iterator end,iterator dest);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// dest 目标起始迭代器
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int> v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9
5.4.2. replace
功能描述:
- 将容器内指定范围所有的旧元素修改为新元素
函数原型:
replace(iterator beg,iterator end, oldvalue, newvalue);
// 将区间内旧元素 替换成 新元素
// beg 开始迭代器
// end 结束迭代器
// oldvalue 旧元素
// newvalue 新元素
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
replace(v.begin(), v.end(), 3, 100);
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 2 100 4 5 6 7 8 9
5.4.3. replace_if
功能描述:
- 将区间内满足条件的元素,替换成指定元素
函数原型:
replace if(iterator beg, iterator end, _pred, newvalue);
// 按条件替换元素,满足条件的替换成指定元素
// beg 开始迭代器
// end 结束迭代器
// _pred 谓词
// newvalue 替换的新元素
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
class Greater5
{
public:
bool operator()(int val)
{
return val > 5;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{
v.push_back(i);
}
replace_if(v.begin(), v.end(), Greater5(), 100);
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 100 100 100 100
5.4.4. swap
功能描述:
- 互换两个容器的元素
函数原型:
swap(container c1,container c2);
// 互换两个容器的元素
// c1容器1
// c2容器2
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
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);
}
cout << "交换前:" << endl;
for_each(v1.begin(), v1.end(), MyPrint());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
swap(v1, v2);
cout << "交换后:" << endl;
for_each(v1.begin(), v1.end(), MyPrint());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
交换前:
0 1 2 3 4 5 6 7 8 9 100 101 102 103 104 105 106 107 108 109
交换后:
100 101 102 103 104 105 106 107 108 109 0 1 2 3 4 5 6 7 8 9
5.5. 常用算术生成算法
学习目标:
- 掌握常用的算术生成算法
注意:
- 算术生成算法属于小型算法,使用时包含的头文件为
#include<numeric>
算法简介:
accumulate
// 计算容器元素累计总和fill
// 向容器中添加元素
5.5.1. accumulate
功能描述:
- 计算区间内 容器元素累计总和
函数原型:
accumulate(iterator beg,iterator end,value);
// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 起始值
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
// 查找内置数据类型
void Test01()
{
vector<int> v;
for(int i = 0; i <= 100; i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(), 0);
cout << total << endl;
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
5050
5.5.2. fill
功能描述:
- 向容器中填充指定的元素
函数原型:
fill(iterator beg,iterator end, value);
// 向容器中填充元素
// beg 开始迭代器
// end 结束迭代器
// value 填充的值
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void Test01()
{
vector<int> v;
// 初始化容器大小为10,并全部填充为0
v.resize(10);
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
// 重新填充
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), MyPrint());
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 0 0 0 0 0 0 0 0 0
100 100 100 100 100 100 100 100 100 100
5.6. 常用集合算法
学习目标:
- 掌握常用的集合算法
算法简介:
set_intersection
// 求两个容器的交集set_union
// 求两个容器的并集set_difference
// 求两个容器的差集
5.6.1. set_intersection
功能描述:
- 求两个容器的交集
函数原型:
set_intersection(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的交集
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
总结:
- 求交集的两个集合必须是有序序列
- 目标容器开辟的空间为两个容器中
size
最小的哪一个 set_intersection
的返回值是交集迭代器中最后一个元素的位置
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
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); // 0~9
v2.push_back(i + 5); // 5~14
}
vector<int> vTarget;
// 目标容器需要提前开辟空间
// 最特殊情况:大容器完全包含小容器,开辟空间时取小容器的size即可
vTarget.resize(min(v1.size(), v2.size()));
// 获取交集
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
for_each(vTarget.begin(), itEnd, MyPrint());
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
5 6 7 8 9 0 0 0 0 0
5 6 7 8 9
5.6.2. set_union
功能描述:
- 求两个集合的并集
函数原型:
set_union(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的并集
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
总结:
- 求并集的两个集合必须是有序序列
- 目标容器开辟的空间为两个容器的
size
相加 set_intersection
的返回值是交集迭代器中最后一个元素的位置
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
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); // 0~9
v2.push_back(i + 5); // 5~14
}
vector<int> vTarget;
// 目标容器需要提前开辟空间
// 最特殊情况:大容器与小容器完全不重合,开辟空间时取两个容器size之和
vTarget.resize(v1.size() + v2.size());
// 获取并集
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
for_each(vTarget.begin(), itEnd, MyPrint());
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5.6.3. set_difference
功能描述:
- 求两个集合的差集
函数原型:
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的差集
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
总结:
- 求并集的两个集合必须是有序序列
- 目标容器开辟的空间为两个容器的
size
相加 set_intersection
的返回值是交集迭代器中最后一个元素的位置
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
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); // 0~9
v2.push_back(i + 5); // 5~14
}
vector<int> vTarget;
// 目标容器需要提前开辟空间
// 最特殊情况:大容器与小容器完全不重合,开辟空间时取两个容器size之和
vTarget.resize(max(v1.size(), v2.size()));
// 获取并集
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
cout << "v1和v2的差集为:" << endl;
for_each(vTarget.begin(), itEnd, MyPrint());
cout << endl;
cout << "v2和v1的差集为:" << endl;
itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, MyPrint());
}
int main()
{
Test01();
return 0;
}
输出:
-----------------------------------------------------------------------------------------
v1和v2的差集为:
0 1 2 3 4
v2和v1的差集为:
10 11 12 13 14