C++ STL常用算法 学习总结

STL常用算法

概述:
算法主要是由头文件< algorithm > < functional > < numeric > 组成。

  • < algorithm > 是所有STL头文件中最大的一个,反包围涉及到比较、交换、查找、遍历操作、复制、修改等等。
  • < numeric > 体积很小,只包括几个在序列上进行简单数学运算的模板函数。
  • < functional > 定义了一些模板类,用于声明函数对象。

find // 查找元素
find_if // 按条件查找元素
adjacent_find // 查找相邻重复元素
binary_search // 二分查找法
count // 统计元素个数
count_if // 按条件统计元素个数

sort // 对容器内元素进行排序
random_shuffle // 洗牌,指定范围内的元素随机调整次序
merge // 容器元素合并,并存储到另一容器中
reverse // 反转指定范围的元素

copy // 容器内指定范围的元素拷贝到另一容器中
replace // 将容器内指定范围的旧元素修改为新元素
replace_if // 容器内指定范围满足条件的元素替换为新元素
swap // 互换两个容器的元素

accumulate // 计算容器元素累计总和
fill // 向容器中添加元素

set_intersection // 求两个容器的交集
set_union // 求两个容器的并集
set_difference // 求两个容器的差集

STL常用算法&遍历算法:for_each()

函数原型:
for_each(iterator beg,iterator end,_func);
遍历算法:遍历容器元素
beg:开始迭代器,end:结束迭代器,_func:函数或者函数对象

案例代码:

#include <vector>
#include <algorithm>
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << "\t";
	}
};
void printVector(const vector<int>& v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
		cout << *it << "\t";
	cout << endl;
}
void test()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	cout << "printVector函数遍历:" << endl;
	printVector(v);
	cout << "for_each函数遍历:" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&遍历算法:transform()

函数原型:
transform(iterator beg1,iterator end1,iterator beg2,_func);
beg1 源容器开始迭代器,end1 源容器结束迭代器
beg2 目标容器开始迭代器,_func 函数或函数对象

案例代码:

#include <vector>
#include <algorithm>
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << "\t";
	}
};
class TransForm
{
public:
	int operator()(int val)
	{
		return val;
	}
};
void test()
{
	vector<int> v;
	for (int i = 0; i < 5; i++)
		v.push_back(i + 1);
	vector<int> vTarget; // 目标容器
	vTarget.resize(v.size()); // 指定目标容器大小
	cout << "vector<int> v:";
	for_each(v.begin(),v.end(),MyPrint());
	cout << endl;
	transform(v.begin(),v.end(),vTarget.begin(),TransForm());
	cout << "vector<int> vTarget:";
	for_each(vTarget.begin(), vTarget.end(), MyPrint());
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

注意:目标容器需要在创建容器后,指定大小,否则无法接收对象。

输出结果:
在这里插入图片描述

STL常用算法&查找算法:find()

函数原型:
find(iterator beg,iterator end,value);
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
beg 开始迭代器,end 结束迭代器,value 查找的元素

案例代码:

#include <vector>
#include <algorithm>
class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age) :m_Name(name), m_Age(age) {}
	bool operator==(const Person& p) // 在find函数内要比对两个参数,需要重载==,否则编译器不知道怎么比对数据
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return true;
		else
			return false;
	}
};
void printVector(const vector<int> &v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
		cout << *it << "\t";
	cout << endl;
}
void printVector(const vector<Person>& v)
{
	for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++)
		cout << "name = " << (*it).m_Name << ",age = " << (*it).m_Age << endl;
	cout << endl;
}
void test()
{
	// 1、内置数据类型查找
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	vector<int>::iterator pos = find(v.begin(),v.end(),5);
	printVector(v);
	if (pos == v.end())
		cout << "没有找到" << endl;
	else
		cout << "找到该元素为:" << *pos << endl;

	// 2、自定义数据类型
	Person p1("wjc",23);
	Person p2("syh", 22);
	Person p3("hmm", 21);
	vector<Person> vp;
	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	printVector(vp);
	Person findPerson("syh",22);
	vector<Person>::iterator pos_Person = find(vp.begin(),vp.end(),findPerson); // 将findPerson对象作为查找比对的对象传入
	if (pos_Person == vp.end())
		cout << "未找到此人" << endl;
	else
		cout << "查找到此人为:" << "name = " << (*pos_Person).m_Name << ",age = " << (*pos_Person).m_Age << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

注意:在自定义数据类型查找时,在find函数内要比对两个参数,需要重载==,否则编译器不知道怎么比对数据。

输出结果:
在这里插入图片描述

STL常用算法&查找算法:find_if()

函数原型:
find_if(iterator beg,iterator end,_Pred);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器,end 结束迭代器,_Pred 函数或者谓词(返回bool类型的仿函数)

案例代码:

#include <vector>
#include <algorithm>
class GreaterFive_Pred
{
public:
	bool operator()(int val)
	{
		return (val > 5);
	}
};
class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age) :m_Name(name), m_Age(age) {}
};
class GreaterTwenty_Pred
{
public:
	bool operator()(Person& p)
	{
		if (p.m_Age > 20)
			return true;
		else
			return false;
	}
};
void printVector(const vector<int> &v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
		cout << *it << "\t";
	cout << endl;
}
void printVector(const vector<Person>& vp)
{
	for (vector<Person>::const_iterator it = vp.begin(); it != vp.end(); it++)
		cout << "name = " << (*it).m_Name << ",age = " << (*it).m_Age << endl;
}
void test()
{
	// 1、内置数据类型查找
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	printVector(v);
	vector<int>::iterator it =  find_if(v.begin(),v.end(),GreaterFive_Pred()); // 查找大于5的数
	if (it == v.end())
		cout << "未找到>5的数据" << endl;
	else
		cout << "找到了>5的数据:" << *it << endl;
	// 2、自定义数据类型查找
	vector<Person> vp;
	Person p1("aaa",10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	printVector(vp);
	vector<Person>::iterator it_Person = find_if(vp.begin(),vp.end(),GreaterTwenty_Pred()); // 查找年龄大于20的对象
	if (it_Person == vp.end())
		cout << "未找到年龄>20的对象" << endl;
	else
		cout << "找到了年龄>20的对象,name = " << (*it_Person).m_Name << ",age = " << (*it_Person).m_Age << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&查找算法:adjacent_find()

函数原型:
adjacent_find(iterator beg,iterator beg);
查找相邻重复元素,返回相邻元素的第一个位置的迭代器,找不到返回end()
beg 开始迭代器,end 结束迭代器

案例代码:

#include <vector>
#include <algorithm>
void test()
{
	vector<int> v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(4);
	v.push_back(5);
	v.push_back(7);
	v.push_back(7);
	vector<int>::iterator pos =  adjacent_find(v.begin(),v.end());
	if (pos == v.end())
		cout << "未查找到元素" << endl;
	else
		cout << "查找到元素:" << *pos << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&查找算法:binary_search()

函数原型:
bool binary_search(iterator beg,iterator end,value);
查找指定的元素,查到返回true,否则false
注意:在无序序列中不可用
beg 开始迭代器,end 结束迭代器,value 查找的元素

案例代码:

#include <vector>
#include <algorithm>
void printVector(const vector<int>& v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
		cout << *it << "\t";
	cout << endl;
}
void test()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	//v.push_back(2);
	// 如果是无序序列,则结果未知。注意:容器必须是有序序列
	printVector(v);
	bool ret = binary_search(v.begin(),v.end(),5);
	if (ret)
		cout << "找到该元素" << endl;
	else
		cout << "未找到该元素" << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&查找算法:count()

count(iterator beg,iterator end,value);
统计元素出现次数
beg 开始迭代器,end 结束迭代器,value 统计的元素

案例代码:

#include <vector>
#include <algorithm>
class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age) :m_Name(name), m_Age(age) {}
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return true;
		else
			return false;
	}
};
void test()
{
	// 1、内置数据类型统计
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	v.push_back(3);
	int num = count(v.begin(),v.end(),3);
	cout << "3在容器中出现的次数 = " << num << endl;
	// 2、自定义数据类型统计
	vector<Person> vp;
	Person p1("孙悟空",999);
	Person p2("猪八戒", 888);
	Person p3("孙悟空", 999);
	Person p4("沙僧", 777);
	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	int num_Person = count(vp.begin(),vp.end(),Person("孙悟空",999)); // 参数3为匿名对象
	cout << "Person(\"孙悟空\",999)在容器中出现的次数 = " << num_Person << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&查找算法:count_if()

count_if(iterator beg,iterator end,_Pred);
按条件统计元素出现次数
beg 开始迭代器,end 结束迭代器,_Pred 谓词

案例代码:

#include <vector>
#include <algorithm>
class GreaterFivePred
{
public:
	bool operator()(int val)
	{
		return (val > 5);
	}
};
class Person
{
public:
	string m_Name;
	int m_Age;
	Person(string name, int age) :m_Name(name), m_Age(age) {}
};
class GreaterEightHundred
{
public:
	bool operator()(const Person& p)
	{
		return (p.m_Age > 800);
	}
};
void test()
{
	// 1、内置数据类型统计
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	int num = count_if(v.begin(), v.end(), GreaterFivePred()); // 统计大于5的数字出现的次数
	cout << ">5在容器中出现的次数 = " << num << endl;
	// 2、自定义数据类型统计
	vector<Person> vp;
	Person p1("孙悟空", 999);
	Person p2("猪八戒", 888);
	Person p3("孙悟空", 999);
	Person p4("沙僧", 777);
	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	int num_Person = count_if(vp.begin(), vp.end(), GreaterEightHundred()); // 统计年龄大于800的对象出现的次数
	cout << "年龄大于800的对象在容器中出现的次数 = " << num_Person << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&排序算法:sort()

sort(iterator beg,iterator end,_Pred)
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器,end 结束迭代器,_Pred 谓词

案例代码:

#include <vector>
#include <algorithm>
#include <functional>
void MyPrint(int val)
{
	cout << val << "\t";
}
bool MyGreater(int val1,int val2)
{
	if (val1 > val2)
		return true;
	else
		return false;
}
void test()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);
	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;
	sort(v.begin(), v.end(), MyGreater); // 使用自定义谓词,按降序排序
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&排序算法:random_shuffle()

random_shuffle(iterator beg,iterator end);
指定范围内的元素随机调整次序
beg 开始迭代器,end 结束迭代器

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "\t";
}
void test()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	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()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&排序算法:merge()

merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
容器元素合并,并存储到另一容器中,新容器的元素依然是有序的
注意:两个容器必须是有序的
beg1 容器1开始迭代器,end1 容器1结束迭代器,beg2 容器2开始迭代器,end2 容器2结束迭代器
dest 目标容器开始迭代器

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v,v2;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
		v2.push_back(i + 1);
	}
	vector<int> vTarget;
	vTarget.resize(v.size() + v2.size()); // 给目标容器分配大小
	merge(v.begin(),v.end(),v2.begin(),v2.end(),vTarget.begin());
	for_each(vTarget.begin(),vTarget.end(),MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&排序算法:reverse()

reverse(iterator beg,iterator end);
反转指定范围的元素 beg 开始迭代器,end 结束迭代器

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
	reverse(v.begin(),v.end());
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&拷贝算法:copy()

函数原型:
copy(iterator beg,iterator end,iterator dest);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg 开始迭代器,end 结束迭代器,dest 目标起始迭代器

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v,vTarget;
	for (int i = 0; i < 5; i++)
		v.push_back(i + 1);
	vTarget.resize(v.size());
	copy(v.begin(),v.end(),vTarget.begin());
	for_each(vTarget.begin(),vTarget.end(),MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&替换算法:replace()

replace(iterator beg,iterator end,oldvalue,newvalue);
将区间内旧元素替换成新元素
beg 开始迭代器,end 结束迭代器,oldvalue 旧元素,newvalue 新元素

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v;
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	cout << "替换前:";
	for_each(v.begin(),v.end(),MyPrint);
	cout << endl;
	cout << "替换后:";
	replace(v.begin(),v.end(),20,2000);
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&替换算法:replace_if()

replace_if(iterator beg,iterator end,_Pred,newvalue);
按条件查找替换元素,满足条件的替换成指定元素
beg 开始迭代器,end 结束迭代器,_Pred 谓词,newvalue 替换的新元素

案例代码:

#include <vector>
#include <algorithm>
void MyPrint(int val)
{
	cout << val << "  ";
}
bool MyPrint_Pred(int val)
{
	if (val >= 30)
		return true;
	else
		return false;
}
void test()
{
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(10);
	v.push_back(40);
	v.push_back(100);
	v.push_back(15);
	v.push_back(60);
	v.push_back(70);
	cout << "替换前:";
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
	cout << "替换后:";
	replace_if(v.begin(), v.end(),MyPrint_Pred,3000); // 将大于等于30的替换成3000
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&交换算法:swap()

swap(container c1,container c2);
互换两个容器的元素
c1 容器1,c2 容器2

案例代码:

#include <vector>
#include <algorithm>
#include <iostream>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v1,v2;
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(i + 1);
		v2.push_back((i + 1) * 10);
	}
	cout << "交换前:" << endl;
	cout << "v1:";
	for_each(v1.begin(),v1.end(),MyPrint);
	cout << endl << "v2:";
	for_each(v2.begin(), v2.end(), MyPrint);

	swap(v1,v2);
	cout << endl << "交换后:" << endl;
	cout << "v1:";
	for_each(v1.begin(), v1.end(), MyPrint);
	cout << endl << "v2:";
	for_each(v2.begin(), v2.end(), MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

注意:swap交换的容器必须是相同类型。

输出结果:
在这里插入图片描述

STL常用算法&算术生成算法:accumulate()

注意:算术生成算法属于小型算法,使用时包含头文件为#include < numeric >。

accumulate(iterator beg,iterator end,value);
计算容器元素累计总和
beg 开始迭代器,end 结束迭代器,value 起始值

案例代码:

#include <vector>
#include <numeric>
void test()
{
	vector<int> v;
	for (int i = 0; i <= 100; i++)
		v.push_back(i);
	int sum = accumulate(v.begin(),v.end(),0);
	cout << "sum = " << sum << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&算术生成算法:fill()

fill(iterator beg,iterator end,value);
向容器中填充元素
beg 开始迭代器,end 结束迭代器,value 填充的值

案例代码:

#include <vector>
#include <algorithm>
#include <numeric>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i + 1);
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;

	fill(v.begin(),v.end(),100);
	for_each(v.begin(),v.end(),MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&集合算法: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>
#include <numeric>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v1, v2,vTarget;
	for (int i = 0; i < 10; i++) 
	{
		v1.push_back(i + 1); // 1~ 10
		v2.push_back(i + 5); // 5 ~ 14
	}
	cout << "v1:";
	for_each(v1.begin(),v1.end(),MyPrint);
	cout << endl;
	cout << "v2:";
	for_each(v2.begin(), v2.end(),MyPrint);
	cout << endl;
	
	vTarget.resize(min(v1.size(),v2.size())); // 设置目前容器大小,交集空间取两个容器空间最小的开辟空间
	vector<int>::iterator itEnd = set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),vTarget.begin());
	// set_intersection函数返回的是交集最后一个元素的迭代器地址
	cout << "vTarget:" << endl;
	for_each(vTarget.begin(), vTarget.end(), MyPrint); // 如果是vTarget.end的话 会输出vTarget容器所有的容器元素
	cout << endl;
	for_each(vTarget.begin(), itEnd, MyPrint); // 如果是交集最后一个迭代器的话,则只会输出vTarget容器里v1、v2交集元素
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&集合算法: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>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v1, v2, vTarget;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1); // 1 ~ 10
		v2.push_back(i + 5); // 5 ~ 14
	}
	cout << "v1:";
	for_each(v1.begin(), v1.end(), MyPrint);
	cout << endl;
	cout << "v2:";
	for_each(v2.begin(), v2.end(), MyPrint);
	cout << endl;

	vTarget.resize(v1.size() + v2.size()); // 设置目前容器大小,并集空间取两个容器空间之和开辟空间
	vector<int>::iterator itEnd = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),vTarget.begin());
	// set_union函数返回的是并集最后一个元素的迭代器地址
	cout << "vTarget:" << endl;
	for_each(vTarget.begin(), vTarget.end(), MyPrint); // 如果是vTarget.end的话 会输出vTarget容器所有的容器元素
	cout << endl;
	for_each(vTarget.begin(), itEnd, MyPrint); // 如果是交集最后一个迭代器的话,则只会输出vTarget容器里v1、v2并集元素
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

STL常用算法&集合算法: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>
void MyPrint(int val)
{
	cout << val << "  ";
}
void test()
{
	vector<int> v1, v2, vTarget;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1); // 1 ~ 10
		v2.push_back(i + 5); // 5 ~ 14
	}
	cout << "v1:";
	for_each(v1.begin(), v1.end(), MyPrint);
	cout << endl;
	cout << "v2:";
	for_each(v2.begin(), v2.end(), MyPrint);
	cout << endl;

	vTarget.resize(max(v1.size(),v2.size())); // 设置目前容器大小,差集空间取两个容器空间最大的开辟空间
	vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	// set_difference函数返回的是差集最后一个元素的迭代器地址
	cout << "vTarget(v1和v2的差集):" << endl;
	for_each(vTarget.begin(), vTarget.end(), MyPrint); // 如果是vTarget.end的话 会输出vTarget容器所有的容器元素
	cout << endl;
	for_each(vTarget.begin(), itEnd, MyPrint); // 如果是交集最后一个迭代器的话,则只会输出vTarget容器里v1、v2差集元素
	cout << endl;

	cout << "vTarget(v2和v1的差集):" << endl;
	itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
	for_each(vTarget.begin(), itEnd, MyPrint);
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值