C++STL常用算法(超详细!)
v1中是有4的,但是它结果显示为未找到,就是因为v1是无序的。,找到返回相邻元素的第一个位置的迭代器,找不到返回结束迭代器。,找到返回指定元素的迭代器,找不到返回结束迭代器end(),找到返回指定元素的迭代器,找不到返回结束迭代器end()注:两个容器必须是有序的,合并完之后也是一个有序序列。,也就是最特殊的那种情况--大容器和小容器无交集。,也就是最特殊的那种情况--大容器和小容器无交集。,也就
目录
基本介绍
- 算法主要是由头文件<algorithm>、<functional>、<numeric>组成
- <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等操作
- <functional>定义了一些模板类,用以声明函数对象
- <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
一、常用遍历算法
- for_each //遍历容器
- transform //搬运容器到另一个容器中
1、for_each
遍历容器
函数原型
for_each(iterator beg,iterator end,_func);-----遍历容器元素
- beg--开始迭代器
- end--结束迭代器
- _func--函数或函数对象
测试
#include<algorithm>
#include<vector>
//遍历for_each
//普通函数
void print1(int val) {
cout << val << " ";
}
//仿函数
class print2 {
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(), print1);//0 1 2 3 4 5 6 7 8 9
cout << endl;
for_each(v.begin(), v.end(), print2()); //print2()--匿名函数对象
//0 1 2 3 4 5 6 7 8 9
cout << endl;
}
int main() {
Test01();
return 0;
}
2、transform
搬运容器到另一个容器中
函数原型
transform(iterator beg1,iterator end1,iterator beg2,_func);
- beg1--源容器开始迭代器
- end1--源容器结束迭代器
- beg2--目标容器开始迭代器
- _func--函数或函数对象
测试
搬运的目标容器必须提前开辟空间,否则无法正常搬运
#include<algorithm>
#include<vector>
//transform--搬运
//仿函数
class Transform {
public:
int operator()(int val) {
return val;
}
};
class Transform1 {
public:
int operator()(int val) {
return val + 100;
}
};
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test02() {
vector<int> v1;//源容器
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
vector<int> v2;//目标容器
v2.resize(v1.size());//在tranform操作之前,目标容器需要提前开辟空间,否则程序会崩溃
//在这里添加仿函数,是可以在仿函数中对源数据进行一些操作之后再搬到目标容器中
//transform(v1.begin(), v1.end(), v2.begin(), Transform());//0 1 2 3 4 5 6 7 8 9
transform(v1.begin(), v1.end(), v2.begin(), Transform1());//100 101 102 103 104 105 106 107 108 109
for_each(v2.begin(), v2.end(), myPrint());//0 1 2 3 4 5 6 7 8 9
}
int main() {
Test02();
return 0;
}
二、常用查找算法
- find //查找元素
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找法
- count //统计元素个数
- count_if //按条件统计元素个数
1、find
按值查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型
find(iterator beg,iterator end,value);
- beg--开始迭代器
- end--结束迭代器
- value--查找的元素
测试
#include<algorithm>
#include<vector>
//find
//查找内置数据类型
void Test03() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//查找容器中是否有5
vector<int>::iterator it=find(v.begin(), v.end(),5);
if (it == v.end()) {
cout << "未找到该元素!" << endl;
}
else {
cout << "找到该元素:" << *it << endl;
}
it = find(v.begin(), v.end(), 40);
if (it == v.end()) {
cout << "未找到该元素!" << endl;
}
else {
cout << "找到该元素:" << *it << endl;
}
}
//查找自定义数据类型
class Person {
public:
string m_Name;
int m_Age;
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;
}
}
};
void Test04() {
vector<Person> v;
Person p1("刘晨", 19);
Person p2("王敏", 20);
Person p3("张立", 17);
Person p4("李四", 24);
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()) {//注意此处:需要重载==,让底层find知道如何对比Person数据类型
cout << "未找到!" << endl;
}
else {
cout << "找到啦!" << endl;
cout << "姓名:" << (*it).m_Name <<"\t" << "年龄:" << (*it).m_Age << endl;
}
//一般更多的使用情况是:找一下容器中有没有和ppp对象相同值的元素,而不是直接去查容器中某个元素
Person ppp("李四", 24);
it = find(v.begin(), v.end(), ppp);
if (it == v.end()) {//注意此处:需要重载==,让底层find知道如何对比Person数据类型
cout << "未找到!" << endl;
}
else {
cout << "找到啦!" << endl;
cout << "姓名:" << (*it).m_Name << "\t" << "年龄:" << (*it).m_Age << endl;
}
}
int main() {
Test03();
Test04();
return 0;
}
- find--在容器中查找指定元素,返回值是迭代器
- 对于查找自定义数据类型来说,需要重载==,让底层find知道如何对比自定义数据类型
2、find_if
按条件查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型
find_if(iterator beg,iterator end,_Pred);
- beg--开始迭代器
- end--结束迭代器
- _Pred--函数或谓词(返回bool类型的仿函数)
- 返回值是迭代器
测试
#include<algorithm>
#include<vector>
#include<string>
//find_if--条件查找
//1、查找内置数据类型
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
void Test05() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//查找容器中是否有大于5的数
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "未找到该元素!" << endl;
}
else {
cout << "找到该元素:" << *it << endl;
}
}
//2、查找自定义数据类型
class Person {
public:
string m_Name;
int m_Age;
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
};
class Greater20 {
public:
bool operator()(Person& p) {
return p.m_Age > 20;
}
};
void Test06() {
vector<Person> v;
Person p1("刘晨", 19);
Person p2("王敏", 20);
Person p3("张立", 17);
Person p4("李四", 24);
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 << "找到啦!" << endl;
cout << "姓名:" << (*it).m_Name << "\t" << "年龄:" << (*it).m_Age << endl;
}
}
int main() {
Test05();
Test06();
return 0;
}
3、adjacent_find
查找相邻重复元素,找到返回相邻元素的第一个位置的迭代器,找不到返回结束迭代器
函数原型
adjacent_find (iterator beg,iterator end,);
- beg--开始迭代器
- end--结束迭代器
- 返回值是迭代器
测试
#include<algorithm>
#include<vector>
//adjacent_find--查找相邻重复元素
void Test07() {
vector<int> v;
v.push_back(1);
v.push_back(0);
v.push_back(2);
v.push_back(4);
v.push_back(0);
v.push_back(3);
v.push_back(4);
v.push_back(4);
//查找容器中是否有相邻重复元素
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end()) {
cout << "未找到相邻重复元素!" << endl;
}
else {
cout << "找到相邻重复元素:" << *it << endl;
}
}
int main() {
Test07();
return 0;
}
4、binary_search
查找指定元素是否存在,查到返回true,否则返回false
查找效率很高!
函数原型
binary_search (iterator beg,iterator end,value);
- beg--开始迭代器
- end--结束迭代器
- value--查找的元素
- 在无序序列中不可用,如果是无序序列,则结果未知
- 返回值是bool型
测试
#include<algorithm>
#include<vector>
//binary_search--查找指定元素是否存在
void Test08() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//查找容器中是否有4
//注:容器必须是有序序列
bool ret=binary_search(v.begin(), v.end(), 4);
if (ret) {
cout << "找到该元素!" << endl;
}
else {
cout << "未找到该元素!" << endl;
}
//如果是无序序列,结果未知!
vector<int> v1;
v1.push_back(0);
v1.push_back(2);
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(4);
v1.push_back(4);
v1.push_back(1);
ret = binary_search(v1.begin(), v1.end(), 4);
if (ret) {
cout << "找到该元素!" << endl;
}
else {
cout << "未找到该元素!" << endl;
}
}
int main() {
Test08();
return 0;
}
v1中是有4的,但是它结果显示为未找到,就是因为v1是无序的
5、count
统计元素个数
函数原型
count(iterator beg,iterator end,value);
- beg--开始迭代器
- end--结束迭代器
- value--统计的元素
测试
#include<algorithm>
#include<vector>
//count--统计元素个数
//1、内置数据类型
void Test09() {
vector<int> v1;
v1.push_back(0);
v1.push_back(2);
v1.push_back(4);
v1.push_back(3);
v1.push_back(5);
v1.push_back(4);
v1.push_back(4);
v1.push_back(1);
int num=count(v1.begin(), v1.end(), 4);
cout << "4的个数为:" << num << endl;//3
}
//2、自定义数据类型
class Person {
public:
string m_Name;
int m_Age;
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person& p) const {
if (this->m_Age == p.m_Age) {
return true;
}
else {
return false;
}
}
};
void Test10() {
vector<Person> v;
Person p1("刘晨", 24);
Person p2("王敏", 20);
Person p3("张立", 19);
Person p4("李四", 24);
Person p5("张三", 24);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//统计一下和rr年龄相同的人有几个
//需要重载==
Person pp("hh", 24);
int num=count(v.begin(), v.end(), pp);
cout << "和hh年龄相同的人的个数为:" << num << endl;//3
}
int main() {
Test09();
Test10();
return 0;
}
对于自定义数据类型来说,统计的时候需要有重载==
6、count_if
按条件统计元素个数
函数原型
count_if (iterator beg,iterator end,_Pred);
- beg--开始迭代器
- end--结束迭代器
- _Pred--谓词(返回bool类型的仿函数)
测试
#include<algorithm>
#include<vector>
//count_if--按条件统计元素个数
//1、内置数据类型
class greater3 {
public:
bool operator()(int val){
return val > 3;
}
};
void Test11() {
vector<int> v1;
v1.push_back(0);
v1.push_back(2);
v1.push_back(4);
v1.push_back(3);
v1.push_back(5);
v1.push_back(4);
v1.push_back(4);
v1.push_back(1);
//统计大于3的数有多少个
int num = count_if(v1.begin(), v1.end(),greater3());//通过仿函数指定规则
cout << "大于3的元素个数为:" << num << endl;//4
}
//2、自定义数据类型
class Person {
public:
string m_Name;
int m_Age;
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
};
class greater20 {
public:
bool operator()(const Person& p)const {
return p.m_Age > 20;
}
};
void Test12() {
vector<Person> v;
Person p1("刘晨", 24);
Person p2("王敏", 20);
Person p3("张立", 19);
Person p4("李四", 24);
Person p5("张三", 24);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//统计一下年龄大于20的人有几个
int num = count_if(v.begin(), v.end(), greater20());
cout << "年龄大于20的人的个数为:" << num << endl;//3
}
int main() {
Test11();
Test12();
return 0;
}
三、常用排序算法
- sort //对容器内元素进行排序
- random_shuffle //洗牌 指定范围内的元素随机调整顺序
- merge //容器元素合并,并存储到另一容器中
- reverse //反转指定范围内的元素
1、sort
对容器内元素排序
函数原型
sort(iterator beg,iterator end,_Pred);
//第三个参数可写可不写,如果有自定义排序规则,就需要写谓词
- beg--开始迭代器
- end--结束迭代器
- _Pred--谓词(返回bool类型的仿函数)
测试
#include<algorithm>
#include<vector>
#include<functional>
//sort
void myPrint(int val) {
cout << val << " ";
}
class myCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void Test13() {
vector<int> v1;
v1.push_back(0);
v1.push_back(2);
v1.push_back(4);
v1.push_back(3);
v1.push_back(5);
v1.push_back(4);
v1.push_back(4);
v1.push_back(1);
//默认为升序
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), myPrint);//0 1 2 3 4 4 4 5
cout << endl;
//改为降序
//sort(v1.begin(), v1.end(), myCompare());//自定义降序仿函数
sort(v1.begin(), v1.end(), greater<int>());//使用内建仿函数
for_each(v1.begin(), v1.end(), myPrint);//5 4 4 4 3 2 1 0
}
int main() {
Test13();
return 0;
}
2、random_shuffle
洗牌 指定范围内的元素随机调整顺序
函数原型
random_shuffle(iterator beg,iterator end);
- beg--开始迭代器
- end--结束迭代器
测试
#include<algorithm>
#include<vector>
#include<ctime>
//random_shuffle
void myPrint(int val) {
cout << val << " ";
}
void Test14() {
srand((unsigned int)time(NULL));//加上随机数种子之后,每一次随机打乱的结果就都是不一样的
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
//利用洗牌算法 打乱顺序
random_shuffle(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), myPrint);//8 1 9 2 0 5 7 3 4 6
cout << endl;
}
int main() {
Test14();
return 0;
}
如果不加随机数种子的话,每次的执行结果都是8 1 9 2 0 5 7 3 4 6
加上之后,每次的执行结果都不一样
3、merge
两个容器元素合并,并存储到另一容器中
函数原型
merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
- beg1--容器1开始迭代器
- end1--容器1结束迭代器
- beg2--容器2开始迭代器
- end2--容器2结束迭代器
- dest--目标容器开始迭代器
- 注:两个容器必须是有序的,合并完之后也是一个有序序列
- 目标容器需要提前开辟空间
测试
#include<algorithm>
#include<vector>
//merge
void myPrint(int val) {
cout << val << " ";
}
void Test15() {
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 1);
}
vector<int> v;
//目标容器需要提前开辟空间
v.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), v.end(), myPrint);//0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
cout << endl;
}
int main() {
Test15();
return 0;
}
4、reverse
将容器内元素反转
函数原型
reverse(iterator beg,iterator end);
- beg--开始迭代器
- end--结束迭代器
测试
#include<algorithm>
#include<vector>
//reverse
void myPrint(int val) {
cout << val << " ";
}
void Test16() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
cout << "反转前:" << endl;
for_each(v1.begin(), v1.end(), myPrint);//0 1 2 3 4 5 6 7 8 9
reverse(v1.begin(), v1.end());
cout << endl;
cout << "反转后:" << endl;
for_each(v1.begin(), v1.end(), myPrint);//9 8 7 6 5 4 3 2 1 0
cout << endl;
}
int main() {
Test16();
return 0;
}
四、常用拷贝和替换算法
- copy //容器内指定范围内元素拷贝到另一容器中
- replace //将容器内指定范围的旧元素修改为新元素
- replace_if //容器内指定范围满足条件的元素替换为新元素
- swap //互换两个容器的元素
1、copy
容器内指定范围内元素拷贝到另一容器中
函数原型
copy(iterator beg,iterator end,iterator dest);
- beg--开始迭代器
- end--结束迭代器
- dest--目标起始迭代器
- 目标容器需要提前开辟空间
测试
#include<algorithm>
#include<vector>
//copy
void myPrint(int val) {
cout << val << " ";
}
void Test17() {
vector<int> v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
vector<int> v;
v.resize(v1.size());
copy(v1.begin(), v1.end(), v.begin());
for_each(v.begin(), v.end(), myPrint);//0 1 2 3 4 5 6 7 8 9
cout << endl;
}
int main() {
Test17();
return 0;
}
2、replace
将容器内指定范围的旧元素修改为新元素
函数原型
replace(iterator beg,iterator end,oldvalue,newvalue);
- beg--开始迭代器
- end--结束迭代器
- oldvalue--旧元素
- newvalue--新元素
测试
#include<algorithm>
#include<vector>
//replace
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
vector<int> v1;
v1.push_back(20);
v1.push_back(50);
v1.push_back(30);
v1.push_back(20);
v1.push_back(20);
v1.push_back(40);
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint());//20 50 30 20 20 40
cout << endl;
replace(v1.begin(), v1.end(), 20, 200);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), myPrint());//200 50 30 200 200 40
cout << endl;
}
int main() {
Test();
return 0;
}
3、replace_if
将容器内指定范围满足条件的元素替换为新元素
函数原型
replace_if(iterator beg,iterator end,_Pred,newvalue);
- beg--开始迭代器
- end--结束迭代器
- _Pred--谓词
- newvalue--替换的新元素
测试
#include<algorithm>
#include<vector>
//replace_if
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
class greater30 {
public:
bool operator()(int val) {
return val > 30;
}
};
void Test() {
vector<int> v1;
v1.push_back(20);
v1.push_back(50);
v1.push_back(30);
v1.push_back(20);
v1.push_back(20);
v1.push_back(40);
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint());//20 50 30 20 20 40
cout << endl;
replace_if(v1.begin(), v1.end(),greater30(), 400);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), myPrint());//20 400 30 20 20 400
cout << endl;
}
int main() {
Test();
return 0;
}
4、swap
互换两个容器的元素
函数原型
swap(container c1,container c2);
- c1--容器1
- c2--容器2
- 交换的容器要是同类型的
测试
#include<algorithm>
#include<vector>
//swap
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
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());//0 1 2 3 4 5 6 7 8 9
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());//100 101 102 103 104 105 106 107 108 109
cout << endl;
cout << "互换后:" << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), myPrint());//100 101 102 103 104 105 106 107 108 109
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());//0 1 2 3 4 5 6 7 8 9
cout << endl;
}
int main() {
Test();
return 0;
}
五、常用算术生成算法
算术生成算法属于小型算法,使用时包含的头文件为<numeric>
- accumulate //计算区间内容器元素累计总和
- fill //向容器中填充指定元素
1、accumulate
计算区间内容器元素累计总和
函数原型
accumulate(iterator beg,iterator end,value);
- beg--开始迭代器
- end--结束迭代器
- value--起始累加值
测试
#include<vector>
#include<numeric>
//accumulate
void Test() {
vector<int> v;
for (int i = 0; i <= 100; i++) {
v.push_back(i);
}
//参数3是一个起始累加值
int total=accumulate(v.begin(), v.end(), 0);
cout << total << endl;//5050
}
int main() {
Test();
return 0;
}
2、fill
向容器中填充指定元素
函数原型
fill(iterator beg,iterator end,value);
- beg--开始迭代器
- end--结束迭代器
- value--填充的值
测试
#include<vector>
#include<algorithm>
#include<numeric>
//fill
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
vector<int> v;
v.resize(10);
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), myPrint());//100 100 100 100 100 100 100 100 100 100
}
int main() {
Test();
return 0;
六、常用集合算法
- 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--目标容器开始迭代器
- 注:两个集合必须是有序序列
- 返回值:交集中最后一个元素的迭代器位置
- 目标容器开辟空间需要从两个容器中取小值,也就是最特殊的那种情况--大容器包小容器
测试
#include<vector>
#include<algorithm>
//set_intersection
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
vector<int> v1;
vector<int> v2;
vector<int> v;//目标容器
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
//目标容器需要提前开辟空间
//最特殊的时候,大容器包小容器,取小容器的size即可
v.resize(min(v1.size(), v2.size()));
//返回目标容器的最后一个元素的迭代器位置
vector<int>::iterator itEnd=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), itEnd, myPrint());//5 6 7 8 9
//for_each(v.begin(), v.end(), myPrint());//5 6 7 8 9 0 0 0 0 0
}
int main() {
Test();
return 0;
}
2、set_union
求两个容器的并集
函数原型
set_union (iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
- beg1--容器1开始迭代器
- end1--容器1结束迭代器
- beg2--容器2开始迭代器
- end2--容器2结束迭代器
- dest--目标容器开始迭代器
- 注:两个集合必须是有序序列
- 返回值:并集中最后一个元素的迭代器位置
- 目标容器开辟空间需要取两个容器的大小之和,也就是最特殊的那种情况--大容器和小容器无交集
测试
#include<vector>
#include<algorithm>
//set_union
class myPrint{
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
vector<int> v1;
vector<int> v2;
vector<int> v;//目标容器
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
//目标容器需要提前开辟空间
//最特殊的时候,大容器和小容器中没有交集
v.resize(v1.size()+v2.size());
//返回目标容器的最后一个元素的迭代器位置
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
for_each(v.begin(), itEnd, myPrint());//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
}
int main() {
Test();
return 0;
}
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<vector>
#include<algorithm>
//set_difference
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void Test() {
vector<int> v1;
vector<int> v2;
vector<int> v;//目标容器
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
//目标容器需要提前开辟空间
//最特殊的时候,大容器和小容器中没有交集,需取两容器大的那个
v.resize(max(v1.size() ,v2.size()));
//返回差集中最后一个元素的迭代器位置
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
cout << "v1和v2的差集为:" << endl;
for_each(v.begin(), itEnd, myPrint());//0 1 2 3 4
cout << endl;
cout << "v2和v1的差集为:" << endl;
itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v.begin());
for_each(v.begin(), itEnd, myPrint());//10 11 12 13 14
}
int main() {
Test();
return 0;
}
有错误欢迎指正!
更多推荐
所有评论(0)