概述:
- 算法主要是由头文件
<algorithm>
<functional>
<numeric>
组成。 <algorithm>
是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等。<numeric>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数。<functional>
定义了一些模板类,用以声明函数对象。
一、常用遍历算法
算法简介:
for_each //遍历容器
transform //搬运容器到另一个容器中
函数原型:
- (1)
for_each(iterator beg,iterator end,_func);
for_each(起始迭代器,结束迭代器,仿函数名称);
代码测试:
#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();
system("pause");
return 0;
}
输出:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
tranform(iterator beg1,iterator end1,iterator beg2,_func);
tranform(源容器开始迭代器,源容器结束迭代器,目标容器开始迭代器,函数或者函数对象)
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class Transform1
{
public:
int operator()(int v)
{
return v + 1000;
}
};
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(), Transform1());
for_each(vTarget.begin(), vTarget.end(),MyPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
请按任意键继续. . .
二、常用查找算法
算法简介:
find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找法
count //统计元素个数
count_if //按条件统计元素个数
函数原型:
- (1)
find(iterator beg,iterator end,value);
find(起始迭代器,结束迭代器,查找的元素); 会返回一个迭代器。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//find
//查找 内置数据类型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中 是否有 5 这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 50);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到:" << *it << endl;
}
}
//查找 自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->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);
Person pp("bbb", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "没找到!" << endl;
}
else
{
cout << "找到元素 姓名为:" << (*it).m_Name << " 年龄为:" << (*it).m_Age << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出:
没有找到!
找到元素 姓名为:bbb 年龄为:20
请按任意键继续. . .
find_if(iterator beg,iterator end, _Pred);
find_if(起始迭代器,结束迭代器,函数或者谓词(返回bool类型的仿函数));
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//查找算法 find_if
//1、查找内置数据类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
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;
}
}
//2、查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Greater20
{
public:
bool operator()(Person& p)
{
return p.m_Age > 20;
}
};
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);
//找出年龄大于20的
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();
system("pause");
return 0;
}
输出:
找到大于5的数字为:6
找到年龄大于20的 姓名为:ccc 年龄为:30
请按任意键继续. . .
adjacent_find(iterator beg,iterator end);
查找相邻重复元素,返回相邻元素的第一个位置的迭代器
adjacent_find(起始迭代器,结束迭代器);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//adjacent_find
void test01()
{
vector<int>v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "未找到相邻重复元素" << endl;
}
else
{
cout << "找到相邻重复元素:" << *pos << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
输出:
找到相邻重复元素:3
请按任意键继续. . .
binary_search(iterator beg,iterator end,value);
binary_search(起始迭代器,结束迭代器,查找的元素);
查找指定元素,查到返回true,否则false。
注意:在无序序列中不可用
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//binary_search,速度快
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//v.push_back(2); //此时无序,找不到元素9
//查找容器中是否有 元素 9
//注意 容器必须是有序的序列,否则输出 未找到
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到元素" << endl;
}
else
{
cout << "未找到!" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
输出:
找到元素
请按任意键继续. . .
count(iterator beg,iterator end,value);
统计元素出现的次数
count(起始迭代器,结束迭代器,统计的元素);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//1、统计内置数据类型
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
int num = count(v.begin(), v.end(), 40);
cout << "40出现的次数:" << num << endl;
}
//2、统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
//要加const
bool operator==(const Person& p)
{
if (this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("赵云", 35);
Person p4("曹操", 30);
Person p5("张飞", 40);
Person p("诸葛亮", 35);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p);
int num = count(v.begin(), v.end(), p);
cout << "和诸葛亮同岁数的人员个数:" << num << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出:
40出现的次数:3
和诸葛亮同岁数的人员个数:4
请按任意键继续. . .
count_if(iterator beg,iterator end, _Pred);
count_if(起始迭代器,结束迭代器,谓词);
返回一个数值。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法 count_if
//1、统计内置数据类型
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
int num = count_if(v.begin(), v.end(), Greater20());
cout << "大于20的个数:" << num << endl;
}
//2、统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class AgeGreater30
{
public:
bool operator()(const Person& p)
{
return p.m_Age > 30;
}
};
void test02()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("赵云", 35);
Person p4("曹操", 30);
Person p5("张飞", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//统计大于30岁的人员个数
int num = count_if(v.begin(), v.end(), AgeGreater30());
cout << "大于30的人员个数为:" << num << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出:
大于20的个数:4
大于30的人员个数为:4
请按任意键继续. . .
三、常用排序算法
算法简介:
- sort //对容器内元素进行排序
- random_shuffle //洗牌 指定范围内的元素随机调整次数
- merge //容器元素合并,并存储到另一个容器中
- reverse //反转指定范围的元素
1. sort
函数原型:
sort(iterator beg,iterator end,_Pred);
sort(起始迭代器,结束迭代器,谓词)
谓词可以不填充
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);
//利用sort进行升序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
//改为 降序
//使用greater<int>()需要包含头文件functional
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
10 20 30 40 50
50 40 30 20 10
请按任意键继续. . .
2. random_shuffle
函数原型:
random_shuffle(iterator beg,iterator end);
指定范围内的元素随机调整次序
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
#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;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
8 2 5 1 7 9 0 6 4 3
请按任意键继续. . .
//或者
2 0 3 7 1 9 6 8 4 5
请按任意键继续. . .
//或者其他很多情况
总结:使用random_shuffle时记得加上随机数种子
3. merge
函数原型:
merge(iterator beg1, iterator end1, iterator beg2, iterator beg2, iterator end2, iterator dest);
merge(容器1起始迭代器,容器1结束迭代器,容器2起始迭代器,容器2结束迭代器,目标容器开始迭代器);
容器元素合并,并存储到另一个容器中,这两个容器必须都是有序序列,且都是升序,或者都是降序才能合并。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
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();
system("pause");
return 0;
}
输出:
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
请按任意键继续. . .
4. reverse
函数原型:
reverse(iterator beg,iterator end);
反转指定范围的元素。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);
cout << "反转前:";
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
//反转
reverse(v.begin(), v.end());
cout << "反转后:";
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
反转前:10 30 50 20 40
反转后:40 20 50 30 10
请按任意键继续. . .
四、常用拷贝和替换算法
算法简介:
- copy //容器内指定范围的元素拷贝到另一容器中
- replace //将容器指定范围的旧元素修改为新元素
- replace_if //容器内指定范围满足条件的元素替换为新元素
- swap //互换两个容器的元素
1. copy
copy(iterator beg, iterator end, iterator dest);
copy(起始迭代器,结束迭代器,目标起始迭代器);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void MyPrint(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(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出;
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
目标容器记得提前开辟空间。
2. replace
函数原型:
replace(iterator beg, iterator end, oldvalue, newvalue);
将区间内旧元素替换成新元素
replace(起始迭代器,结束迭代器,旧元素,新元素);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v1;
v1.push_back(20);
v1.push_back(30);
v1.push_back(50);
v1.push_back(30);
v1.push_back(40);
v1.push_back(10);
v1.push_back(20);
cout << "替换前:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
//替换
replace(v1.begin(), v1.end(), 30, 100);
cout << "替换后:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
替换前:20 30 50 30 40 10 20
替换后:20 100 50 100 40 10 20
请按任意键继续. . .
3. replace_if
函数原型:
replace_if(iterator beg, iterator end, _pred, newvalue);
replace_if(起始迭代器,结束迭代器,谓词,替换的新元素);
按条件替换元素,满足条件的替换成指定元素
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
//仿函数,利用仿函数进行筛选
class Greater30
{
public:
bool operator()(int v)
{
return v >= 30;
}
};
void test01()
{
vector<int>v1;
v1.push_back(20);
v1.push_back(30);
v1.push_back(50);
v1.push_back(30);
v1.push_back(40);
v1.push_back(10);
v1.push_back(20);
v1.push_back(20);
cout << "替换前:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
//将大于等于30的 替换为 3000
replace_if(v1.begin(), v1.end(), Greater30(), 3000);
cout << "替换后:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
替换前:20 30 50 30 40 10 20 20
替换后:20 3000 3000 3000 3000 10 20 20
请按任意键继续. . .
4. swap
函数原型:
swap(container c1, container c2);
swap(c1容器1,c2容器2);
互换两个同种类型的容器的元素。
代码测试:
#include<iostream>
using namespace std;
#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 + 100);
}
cout << "互换前:" << endl;
cout << "v1:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
cout << "v2:";
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
//互换两个容器元素
swap(v1, v2);
cout << "互换后:" << endl;
cout << "v1:";
for_each(v1.begin(), v1.end(), MyPrint);
cout << endl;
cout << "v2:";
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
互换前:
v1:0 1 2 3 4 5 6 7 8 9
v2:100 101 102 103 104 105 106 107 108 109
互换后:
v1:100 101 102 103 104 105 106 107 108 109
v2:0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
五、常用算术生成算法
算法简介:
- accumulate //计算容器元素累计总和
- fill //向容器中添加元素
1. accumulate
函数原型:
accumulate(iterator beg, iterator end, value);
accumulate(起始迭代器,结束迭代器,起始值);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//要添加头文件#include<numeric>
int total = accumulate(v.begin(), v.end(), 0);
cout << "元素总和为:" << total << endl; //元素总和为:45
//参数3 是 起始累加值
total = accumulate(v.begin(), v.end(), 1000);
cout << "元素总和为:" << total << endl; //元素总和为:1045
}
int main() {
test01();
system("pause");
return 0;
}
2. fill
函数原型:
fill(iterator beg, iterator end, value);
fill(起始迭代器,结束迭代器,填充的值);
向容器中填充元素
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void MyPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.resize(10);
//后期填充
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
100 100 100 100 100 100 100 100 100 100
请按任意键继续. . .
六、常用集合算法
算法简介:
set_intersection //求两个容器的交集
set_union //求两个容器的并集
set_difference //求两个容器的差集
1. set_intersection
函数原型:
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
求两个容器的交集,会返回一个迭代器,这个迭代器是交集结束时的迭代器。
【注意】两个容器必须是有序序列
set_intersection(容器1起始迭代器,容器1结束迭代器,容器2起始迭代器,容器2结束迭代器,目标容器起始迭代器);
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#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); //0~9
v2.push_back(i + 5); //5~14
}
vector<int>vTarget;
//目标容器需要提前开辟空间
//最特殊的情况 大容器包含小容器 开辟空间 取小容器的size即可
//min需要包含 algorithm
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(), itEnd, MyPrint); // 5 6 7 8 9
cout << endl;
for_each(vTarget.begin(), vTarget.end(), MyPrint); // 5 6 7 8 9 0 0 0 0 0
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
5 6 7 8 9
5 6 7 8 9 0 0 0 0 0
请按任意键继续. . .
2. set_union
函数原型:
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
set_intersection(容器1起始迭代器,容器1结束迭代器,容器2起始迭代器,容器2结束迭代器,目标容器起始迭代器);
求两个集合的并集
【注意】两个集合必须是有序序列
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#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); //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());
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 0
for_each(vTarget.begin(), vTarget.end(), MyPrint);
cout << endl;
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
for_each(vTarget.begin(), itEnd, MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
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
请按任意键继续. . .
总结:
- 求集合的两个集合必须是有序序列
- 目标容器开辟空间需要两个容器相加
- set_union返回值即是并集最后更一个元素的位置
3. set_difference
函数原型:
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
set_difference(容器1起始迭代器,容器1结束迭代器,容器2起始迭代器,容器2结束迭代器,目标容器起始迭代器);
求两个集合的差集
【注意】两个集合必须是有序序列。
代码测试:
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#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); //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());
//0 1 2 3 4 0 0 0 0 0
for_each(vTarget.begin(), vTarget.end(), MyPrint);
cout << endl;
//0 1 2 3 4
for_each(vTarget.begin(), itEnd, MyPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
0 1 2 3 4 0 0 0 0 0
0 1 2 3 4
请按任意键继续. . .
总结:
- 求差集的两个集合必须是有序序列
- 目标容器开辟空间需要从两个容器取size较大值
- set_difference返回值即是差集中最后一个元素的位置