C++笔记9:C++提高编程3:STL---函数对象&标准算法

0904

1、模板

函数模板和类模板

2、初识STL & 3、STL—常用容器(3.1-3.8)

初始STL & STL—常用容器

3.9 map multimap容器(二叉树结构—自动排序)

(有区别)构造和赋值:set<int,int> m1;//默认构造函数 set<int,int> m2(m1);//拷贝构造函数 m3 = m1;//赋值
大小和交换:m1.size();//大小 m1.empty();//判空 m2.swap(m1);//交换
(有区别)插入和删除:m.insert(make_pair(6,25));//四种插入方式,推荐第二种 m.erase();//删除 m.clear();//清空
(有区别)查找和统计:m1.find(6);//返回的是迭代器 m1.count(6);//返回的是个数,找到就是1没找到就是0 注意:这里是按照Key值进行查找和统计的
排序:(和set容器类似)自动排序,既不是成员函数,也不是全局函数;可以利用仿函数自定义排序规则。

3.9.1 map 基本概念

map中所有元素都是pair对组:其中第一个元素为key(键值),起到索引作用,第二个元素为value(实值),可以根据key值快速找到value值。
map容器中的所有元素都会根据元素的键值Key自动排序!!!

map/multimap属于关联式容器,底层结构是用二叉树实现。二者的区别在于map不允许容器中有重复key值元素,而multimap允许(和set/multiset类似)。

3.9.2 构造与赋值 & 3.9.3 map大小和交换 & 3.9.4 map插入和删除 & 3.9.5 map查找和统计

//构造:
map<T1, T2> mp; //map默认构造函数:
map(const map &mp); //拷贝构造函数
//赋值:
map& operator=(const map &mp); //重载赋值操作符
//大小:
size(); //返回容器中元素的数目
empty(); //判断容器是否为空
//交换:
swap(st); //交换两个集合容器
//插入:
insert(elem); //在容器中插入元素。四种插入方式:推荐用放方式二m.insert(make_pair(1,20));和方式一m.insert(pair<int,int>(10,111));,注意方式四不要乱用m[5] = 26;
//删除:
erase(key); //删除容器中值为key的元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
clear(); //清除所有元素。
//查找:
find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
//统计:
count(key); //统计key的元素个数

示例:

//打印map<int,int> m
void printMap(map<int,int>& m) {
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		//cout << "键值Key = " << (*it).first << "\t实值Value = " << (*it).second << endl;
		cout << "键值Key = " << it->first << "\t实值Value = " << it->second << endl;
	}
}

int main() {

	pair<int, int> pr1(1, 10);
	pair<int, int> pr2(4, 20);
	pair<int, int> pr3(3, 30);
	pair<int, int> pr4(5, 40);
	pair<int, int> pr5(2, 50);
//默认构造函数
	map<int,int> m1;
	m1.insert(pr1);//插入
	m1.insert(pr2);
	m1.insert(pr3);
	m1.insert(pr4);
	m1.insert(pr5);
	//map容器会按照键值Key从小到大将元素进行排序:
	printMap(m1);//键值Key = 1     实值Value = 10
				 //键值Key = 2     实值Value = 50
				 //键值Key = 3     实值Value = 30
				 //键值Key = 4     实值Value = 20
				 //键值Key = 5     实值Value = 40
//判空+大小
	if (m1.empty())
		cout << "容器为空!!!" << endl;
	else
		cout << "容器非空,大小为:" << m1.size() << endl;//5
//交换
	pair<int, int> pr11(11, 16);
	pair<int, int> pr12(14, 26);
	pair<int, int> pr13(13, 36);
	pair<int, int> pr14(15, 46);
	pair<int, int> pr15(12, 56);
	
	map<int, int> m4;
	m4.insert(pr11);
	m4.insert(pr12);
	m4.insert(pr13);
	m4.insert(pr14);
	m4.insert(pr15);

	cout << "交换前:" << endl;
	printMap(m1);//
	cout << endl;
	printMap(m4);//
	cout << endl;
	m1.swap(m4);//交换
	cout << "交换后:" << endl;
	printMap(m1);//
	cout << endl;
	printMap(m4);//
	cout << endl;

//拷贝构造函数
	map<int, int> m2(m1);
	printMap(m2);//结果如下:
				//键值Key = 11    实值Value = 16
				//键值Key = 12    实值Value = 56
				//键值Key = 13    实值Value = 36
				//键值Key = 14    实值Value = 26
				//键值Key = 15    实值Value = 46
	cout << endl;
//清空
	m2.clear();
//插入
	//插入方式一:(前面的例子用的就是这个)
	m2.insert(pair<int,int>(10,111));
	//插入方式二:(这种比较简单,推荐)
	m2.insert(make_pair(8,126));
	//插入方式三:(不建议)
	m2.insert(map<int,int>::value_type(9,168));
	//插入方式四:不建议用[]来插入数据,但可以用来访问value,比如cout<<m2[10]<<endl;
	m2[6] = 139;

	printMap(m2);//结果如下:
				//键值Key = 6     实值Value = 139
				//键值Key = 8     实值Value = 126
				//键值Key = 9     实值Value = 168
				//键值Key = 10    实值Value = 111
	cout << endl;
	//利用[Key]来输出Value值
	cout << "m2[10] = " << m2[10] << endl;//111
	//如果容器中没有Key为3的元素,就会创建一个元素插到容器中,对应的Value值为0
	//这就是为什么不建议用插入方式四的原因,想要利用[]输出元素的value值也要小心:要确保容器中有对应的Key值
	cout << "m2[3] = " << m2[3] << endl;//0
	printMap(m2);//结果如下:
			//键值Key = 3     实值Value = 0
			//键值Key = 6     实值Value = 139
			//键值Key = 8     实值Value = 126
			//键值Key = 9     实值Value = 168
			//键值Key = 10    实值Value = 111
	cout << endl;


//赋值
	map<int, int> m3;
	m3 = m1;
	printMap(m3);//结果如下:
				//键值Key = 11    实值Value = 16
				//键值Key = 12    实值Value = 56
				//键值Key = 13    实值Value = 36
				//键值Key = 14    实值Value = 26
				//键值Key = 15    实值Value = 46
	cout << endl;
//删除
	//①删除某个元素
	m3.erase(12);
	m3.erase(14);
	m3.erase(3);//没有key值为3的元素,
	printMap(m3);//结果如下:
			//键值Key = 11    实值Value = 16
			//键值Key = 13    实值Value = 36
			//键值Key = 15    实值Value = 46
	cout << endl;
	m3.insert(make_pair(6,20));
	m3.insert(make_pair(9, 139));
	printMap(m3);//结果如下:
			//键值Key = 6     实值Value = 20
			//键值Key = 9     实值Value = 139
			//键值Key = 11    实值Value = 16
			//键值Key = 13    实值Value = 36
			//键值Key = 15    实值Value = 46
	cout << endl;
	//②删除某个位置的元素
	m3.erase(m3.begin());
	printMap(m3);//结果如下:
			//键值Key = 9     实值Value = 139
			//键值Key = 11    实值Value = 16
			//键值Key = 13    实值Value = 36
			//键值Key = 15    实值Value = 46
	cout << endl;
	//③删除某个区间的所有元素---相当于清空
	m3.erase(m3.begin(),m3.end());
	printMap(m3);//结果:空
	cout << endl;

//查找
	m3 = m1;
	printMap(m3);//结果:空
	cout << endl;
	if (m3.find(6) != m3.end())
		cout << "找到了,m3[6] = " << m3[6] << endl;
	else
		cout << "没找到!" << endl;//√
	
	if (m3.find(12) != m3.end())
		cout << "找到了,m3[12] = " << m3[12] << endl;//√ m3[12] = 56
	else
		cout << "没找到!" << endl;



//统计---因为map容器中不能有重复的key值,所以结果只会是0或1
	int res = m3.count(13);
	cout << "Key值为13的元素个数为:" << res << endl;//1
	cout << "Key值为6的元素个数为:" << m3.count(8) << endl;//0

	system("pause");
	return 0;
}

3.9.6 map容器排序
①map容器默认排序规则为 :按照key值进行 从小到大排序
②利用仿函数自定义排序规则—在创建容器的时候就指明排序规则(按Key值降序)
③map容器中的value值是自定义数据类型Person类
④multimap容器—允许有Key值相同的元素

示例:

//(177-285)map容器:排序
/**/
//利用仿函数实现自定义排序规则---按照Key值降序排列
class MySort {
public:
	bool operator()(double a, double b)const {//加const
		if (a > b)
			return 1;
		else
			return 0;
	}
};
//打印map<double,double> m
void printMap(map<double, double>& m) {
	for (map<double, double>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\tValue = " << it->second << endl;
	}
}
//打印map<double,double,MySort> m
void printMap(map<double, double,MySort>& m) {
	for (map<double, double,MySort>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\tValue = " << it->second << endl;
	}
}
//打印map<double,double,greater<>> m
void printMap(map<double, double, greater<>>& m) {
	for (map<double, double, greater<>>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\tValue = " << it->second << endl;
	}
}
//自定义数据类型:
class Person {
public:
	string name;
	int age;
	int height;

	Person(string name, int age, int height) {
		this->name = name;
		this->age = age;
		this->height = height;
	}
};

//打印map<double,Person> m
void printMap(map<double, Person>& m) {
	for (map<double, Person>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\t姓名:" << it->second.name
			<< "\t年龄:" << it->second.age << "\t身高:" << it->second.height << endl;
	}
}
//打印map<double,Person,MySort> m
void printMap(map<double, Person,MySort>& m) {
	for (map<double, Person, MySort>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\t姓名:" << it->second.name
			<< "\t年龄:" << it->second.age << "\t身高:" << it->second.height << endl;
	}
}
//打印map<double,Person,greater<>> m
void printMap(map<double, Person, greater<>>& m) {
	for (map<double, Person, greater<>>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\t姓名:" << it->second.name
			<< "\t年龄:" << it->second.age << "\t身高:" << it->second.height << endl;
	}
}
//打印multimap<double,double> m
void printMap(multimap<double, double>& m) {
	for (multimap<double, double>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "Key = " << it->first << ";\tValue = " << it->second << endl;
	}
}
int main() {

//①默认排序规则---按照Key值从小到大排
	map<double, double> m1;
	m1.insert(make_pair(1.2, 2.5));
	m1.insert(make_pair(6, 3.8));
	m1.insert(make_pair(4, 3.1));
	printMap(m1);//结果如下:
				//Key = 1.2       Value = 2.5
				//Key = 4		  Value = 3.1
				//Key = 6		  Value = 3.8
	cout << endl;

//②自定义排序规则---在创建容器的时候就指明排序规则(按Key值降序)
	map<double, double,MySort> m2;
	m2.insert(make_pair(1.2, 2.5));
	m2.insert(make_pair(6, 3.8));
	m2.insert(make_pair(4, 3.1));
	printMap(m2);//结果如下:
				//Key = 6		  Value = 3.8
				//Key = 4		  Value = 3.1
				//Key = 1.2       Value = 2.5
	cout << endl;
//③使用内建仿函数(大于仿函数greater<>()):按Key值降序
	map<double, double, greater<>> m5;
	m5.insert(make_pair(1.2, 2.5));
	m5.insert(make_pair(6, 3.8));
	m5.insert(make_pair(4, 3.1));
	printMap(m5);//结果如下:
				//Key = 6		  Value = 3.8
				//Key = 4		  Value = 3.1
				//Key = 1.2       Value = 2.5
	cout << endl;

//map容器中的value值是自定义数据类型Person类:
	Person p1("Tom", 23, 175);
	Person p2("Jerry", 25, 163);//26
	Person p3("Reus", 28, 185);
	Person p4("Lucas", 29, 177);//26
	Person p5("Messi", 26, 173);
	
	//④默认排序规则:按Key值升序
	map<double, Person> m3;
	m3.insert(make_pair(1.1, p2));
	m3.insert(make_pair(6.1, p1));
	m3.insert(make_pair(3.1, p3));
	m3.insert(make_pair(9.1, p5));
	m3.insert(make_pair(5.1, p4));
	printMap(m3);//结果如下:
				//Key = 1.1;      姓名:Jerry     年龄:25        身高:163
				//Key = 3.1;      姓名:Reus      年龄:28        身高:185
				//Key = 5.1;      姓名:Lucas     年龄:29        身高:177
				//Key = 6.1;      姓名:Tom       年龄:23        身高:175
				//Key = 9.1;      姓名:Messi     年龄:26        身高:173
	cout << endl;
	//⑤利用仿函数自定义排序规则:按Key值降序
	map<double, Person,MySort> m6;
	m6.insert(make_pair(1.1, p2));
	m6.insert(make_pair(6.1, p1));
	m6.insert(make_pair(3.1, p3));
	m6.insert(make_pair(9.1, p5));
	m6.insert(make_pair(5.1, p4));
	printMap(m6);//结果如下:
				//Key = 9.1;      姓名:Messi     年龄:26        身高:173
				//Key = 6.1;      姓名:Tom       年龄:23        身高:175
				//Key = 5.1;      姓名:Lucas     年龄:29        身高:177
				//Key = 3.1;      姓名:Reus      年龄:28        身高:185
				//Key = 1.1;      姓名:Jerry     年龄:25        身高:163
	cout << endl;
	//⑥内建仿函数(大于仿函数greater<>()):按Key值降序
	map<double, Person, greater<>> m7;
	m7.insert(make_pair(1.1, p2));
	m7.insert(make_pair(6.1, p1));
	m7.insert(make_pair(3.1, p3));
	m7.insert(make_pair(9.1, p5));
	m7.insert(make_pair(5.1, p4));
	printMap(m7);//结果如下:
				//Key = 9.1;      姓名:Messi     年龄:26        身高:173
				//Key = 6.1;      姓名:Tom       年龄:23        身高:175
				//Key = 5.1;      姓名:Lucas     年龄:29        身高:177
				//Key = 3.1;      姓名:Reus      年龄:28        身高:185
				//Key = 1.1;      姓名:Jerry     年龄:25        身高:163
	cout << endl;

//⑦multimap容器---允许有Key值相同的元素
	multimap<double, double> m4;
	m4.insert(make_pair(1.2, 3.5));
	m4.insert(make_pair(6, 3.8));
	m4.insert(make_pair(1.2, 3.1));
	printMap(m4);//结果如下:
				//Key = 1.2;      Value = 3.5
				//Key = 1.2;      Value = 3.1
				//Key = 6;        Value = 3.8
	cout << endl;

	system("pause");
	return 0;
}

3.10 STL案例2

案例需求:
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作;
员工信息有: 姓名 工资组成;部门分为:策划、美术、研发;
随机给10名员工分配部门和工资;
通过multimap进行信息的插入 key(部门编号) value(员工);
分部门显示员工信息。

示例:

#include<iostream>
#include<string>
#include<map>
using namespace std;

//自定义数据类型Person类:
class Person {
public:
	string name;//姓名
	int wage;//工资
	//int sector;//部门(1.策划 2.美术 3.研发)

	Person(string name, int wage) {//, int sector
		this->name = name;
		this->wage = wage;
		//this->sector = sector;
	}
};

//打印multimap<int,Person> m
void printMultimap(multimap<int, Person>& m) {
	cout << "开始打印:(打印说明---部门编号123分别表示1.策划;2.美术;3.研发)" << endl;
	for (multimap<int, Person>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "部门:" << it->first << "\t姓名" << it->second.name << "\t工资:" << it->second.wage << endl;
	}
	cout << "打印完毕!!!\n" << endl;
}
int main() {
	
	multimap<int, Person> m1;//

	char c = 'A';
	srand((unsigned)time(NULL));//这是确保每次重新运行程序的时候,生成的随机数和上次编译运行不一样
	//创建元素+插入容器
	for (int i = 0; i < 10; i++) {
		
		//创建一个Person对象
		string name = "a";
		name[0] = c;
		c++;
		int random_wage = (rand() % (6000 - 3000 + 1) + 3000);//工资随机数[3000,6000]
		Person p(name, random_wage);

		//插入容器
		int random_sector = (rand() % (3 - 1 + 1) + 1);//部门随机数[1,3]
		m1.insert(make_pair(random_sector,p));

	}

	printMultimap(m1);//结果如下:
	//开始打印:(打印说明-- - 部门编号123分别表示1.策划;2.美术;3.研发)
	//部门:1 姓名F   工资:3156
	//部门:1 姓名I   工资:5496
	//部门:1 姓名J   工资:4031
	//部门:2 姓名B   工资:5564
	//部门:2 姓名C   工资:5524
	//部门:2 姓名E   工资:3789
	//部门:2 姓名H   工资:4220
	//部门:3 姓名A   工资:3991
	//部门:3 姓名D   工资:4169
	//部门:3 姓名G   工资:5130
	//打印完毕!!!

	system("pause");
	return 0;
}

4、STL—函数对象(仿函数-重载函数调用运算符()

4.1 函数对象

4.1.1 函数对象基本概念
重载函数调用操作符()的类,其对象常称为函数对象
函数对象在使用重载的函数调用操作符()时,行为类似函数调用,所以也叫仿函数

注意:函数对象(仿函数)是一个,不是一个函数。(和普通函数类似,又超出普通函数的概念)

4.1.2 函数对象的使用
1.函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
2.函数对象又超出普通函数的概念,函数对象可以有自己的状态(因为函数对象是一个类,类就会有成员变量和成员函数等);
3.函数对象可以作为参数传递

示例:

#include<iostream>
#include<string>

using namespace std;

//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
class MyOperator {
public:

	//有参数
	void operator()(string str) {
		cout << "有参数:" << str << endl;
	}

	//有参数,有返回值
	int operator()(int a,int b) {
		cout << "有参数,有返回值:";
		return a + b;
	}
};

//2、函数对象可以有自己的状态
class MyOperator1 {
public:
	//成员变量
	int count;

	//构造函数
	MyOperator1() {
		count = 0;
	}
	
	//成员函数
	void operator()(int n) {

		if (n == 1)
			cout << "正确√" ;
		else if (n == 0)
			cout << "错误!" ;
		else
			cout << "输入有误!!!" ;

		this->count++;
		cout << "\t调用次数: No." << this->count << endl;
	}

};

//3、函数对象可以作为参数传递
void MyJudge(MyOperator1& mtOpe1,int n) {
	
	cout << "函数对象作为参数传递: ";
	mtOpe1(n);

}

int main() {
	//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
	string str = "2021.9.6 星期一 阴天";
	MyOperator myOpe;
	myOpe(str);//有参数:2021.9.6 星期一 阴天
	cout << myOpe(13, 26) << endl;//有参数,有返回值:39

	//2、函数对象可以有自己的状态
	MyOperator1 myOpe1;
	myOpe1(1);//正确√  调用次数: No.1
	myOpe1(0);//错误!  调用次数: No.2
	myOpe1(2);//输入有误!!!  调用次数: No.3

	//3、函数对象可以作为参数传递
	MyJudge(myOpe1, 1);//函数对象作为参数传递: 正确√   调用次数: No.4
	MyJudge(myOpe1, 2);//函数对象作为参数传递: 输入有误!!!   调用次数: No.5
	MyJudge(myOpe1, 0);//函数对象作为参数传递: 错误!   调用次数: No.6

	system("pause");
	return 0;
}

4.2 谓词

4.2.1 谓词基本概念

返回bool类型的仿函数称为谓词
如果operator()接受一个参数,那么叫做一元谓词;
如果operator()接受两个参数,那么叫做二元谓词。

4.2.2 一元谓词

示例:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

class FindFive {
public:
	bool operator()(int val) {//一元谓词:返回值是bool类型;一个参数
		if (val == 5)
			return 1;
		else
			return 0;
	}
};

int main() {

	vector<int> v1;
	for (int i = 0; i < 5; i++) {
		v1.push_back(i + 1);
	}
	for (int i = 0; i < v1.size(); i++) {
		cout << v1[i] << " ";
	}
	cout << endl;

	//使用find_if查找容器中有没有等于5的元素  find_if(_InIt _First, const _InIt _Last, _Pr _Pred)//pred表示谓词
	//FindFive find5;
	//vector<int>::iterator it = find_if(v1.begin(), v1.end(), find5);
	vector<int>::iterator it = find_if(v1.begin(), v1.end(), FindFive());//这里的FindFiver()是匿名函数对象
	if (it == v1.end())
		cout << "没找到!!!" << endl;
	else
		cout << "找到了" << endl;//√

	system("pause");
	return 0;
}

4.2.3 二元谓词

示例:

//打印
void printVector(vector<int>& v1) {
	for (int i = 0; i < v1.size(); i++) {
		cout << v1[i] << " ";
	}
	cout << endl;
}

//二元谓词:
class MySort {
public:

	bool operator()(int a,int b) {
		if (a > b)
			return 1;
		else
			return 0;
	}
};

int main() {

//二元谓词:
	vector<int> v2;
	v2.push_back(10);
	v2.push_back(30);
	v2.push_back(50);
	v2.push_back(20);
	v2.push_back(40);

	//默认排序规则:升序
	sort(v2.begin(), v2.end());
	printVector(v2);//10 20 30 40 50

	//自定义排序规则:降序
	sort(v2.begin(), v2.end(), MySort);//注意:这里是MySort,不是写MySort()
	printVector(v2);//50 40 30 20 10

	system("pause");
	return 0;
}

4.3 内建函数对象

4.3.1 内建函数对象的意义

STL内建了一些函数对象:
算术仿函数
关系仿函数
逻辑仿函数

用法:
这些仿函数所产生的对象,用法和一般函数完全相同;
使用内建函数对象,需要引入头文件 #include<functional>

4.3.2 算术仿函数
返回值是T:
template<class T> T plus<T> //加法仿函数
template<class T> T minus<T> //减法仿函数
template<class T> T multiplies<T> //乘法仿函数
template<class T> T divides<T> //除法仿函数
template<class T> T modulus<T> //取模仿函数
template<class T> T negate<T> //取反仿函数
注意:其中除了negate是一元运算,其他都是二元运算。

4.3.3 关系仿函数
返回值是bool:
template<class T> bool equal_to<T> //等于
template<class T> bool not_equal_to<T> //不等于
template<class T> bool greater<T> //大于
template<class T> bool greater_equal<T> //大于等于
template<class T> bool less<T> //小于
template<class T> bool less_equal<T> //小于等于
注意:关系仿函数中最常用的就是greater<>大于,因为默认排序规则都时升序,想要实现降序排序规则,就要是用大于仿函数greater<>()

4.3.4 逻辑仿函数
返回值是bool:
template<class T> bool logical_and<T> //逻辑与
template<class T> bool logical_or<T> //逻辑或
template<class T> bool logical_not<T> //逻辑非
注意:逻辑仿函数实际应用较少,了解即可。

示例(4.2.2 & 4.2.3 & 4.2.4):

#include<iostream>
#include<string>
#include<functional>//内建函数对象的头文件
#include<vector>
#include<algorithm>

using namespace std;

//4.3.3 关系仿函数:
//打印
void printVector(vector<int>& v1) {
	for (int i = 0; i < v1.size(); i++) {
		cout << v1[i] << " ";
	}
	cout << endl;
}
//二元谓词:返回bool,2个参数
class MySort {
public:

	bool operator()(int a,int b) {
		if (a > b)
			return 1;
		else
			return 0;
	}
};

int main() {

//4.3.2 算术仿函数:加减乘除取模取反
	negate<int> n;
	cout << n(3) << endl;//-3

	plus<int> p;
	cout << p(3, 7) << endl;//10

	minus<double> m;
	cout << m(3.6, 7.9) << endl;//-4.3

	multiplies<float> mu;
	cout << mu(2.5, 4.0) << endl;//10

	divides<int> d;
	cout << d(20, 3) << endl;//6

	modulus<int> mod;
	cout << mod(20, 3) << endl;//2
	
//4.3.3 关系仿函数:
	vector<int> v2;
	v2.push_back(10);
	v2.push_back(30);
	v2.push_back(50);
	v2.push_back(20);
	v2.push_back(40);

	vector<int> v3;
	v3 = v2;
	//使用默认排序规则
	sort(v2.begin(), v2.end());
	printVector(v2);//10 20 30 40 50
	//使用自己实现的仿函数---降序
	sort(v2.begin(), v2.end(),MySort());//注意:这里是MySort(),不是MySort
	printVector(v2);//50 40 30 20 10
	v2 = v3;
	//使用内建仿函数:大于仿函数
	sort(v2.begin(), v2.end(),greater<>());//注意:这里是greater<>() !!!
	printVector(v2);//50 40 30 20 10

//4.3.4 逻辑仿函数:
	vector<bool> v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	v.push_back(false);

	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//1 0 1 0

	//逻辑非  将v容器搬运到v2中,并执行逻辑非运算
	vector<bool> v4;
	v4.resize(v.size());
	transform(v.begin(), v.end(), v4.begin(), logical_not<bool>());//将v容器搬运到v2中,并执行逻辑非运算
	for (vector<bool>::iterator it = v4.begin(); it != v4.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//0 1 0 1

	system("pause");
	return 0;
}

5、STL—常用算法

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

5.1 常用遍历算法

5.1.1 for_each //遍历容器; 2.5.1和2.5.2有用到
5.1.2 transform //搬运容器到另一个容器中;4.3.4逻辑仿函数有用到

5.1.1 for_each & 5.1.2 transform

for_each(iterator beg, iterator end, _func); //
// beg 开始迭代器
// end 结束迭代器
// _func 函数或函数对象(普通的打印函数 或 重载函数调用运算符()
transform(iterator beg1, iterator end1, iterator beg2, _func);//
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
注意:目标容器创建好之后,一定要用v2.resize(v1.size());来重新开辟空间!!!

示例(5.1.1 & 5.1.2):

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<numeric>
#include<vector>

using namespace std;

//5.1.1 for_each
//普通函数
void MyPrint1(int val) {
	cout << val << " ";
}

//函数对象
class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};
//5.1.2 transform
class MyTransform {
public:
	int operator()(int val) {
		return val;
	}
};

int main() {

//5.1.1 for_each
	vector<int> v1;
	for (int i = 0; i < 5; i++) {
		v1.push_back(i + 1);
	}
	//遍历:
	for_each(v1.begin(), v1.end(), MyPrint1);//普通函数
	cout << endl;
	for_each(v1.begin(), v1.end(), MyPrint());//函数对象
	cout << endl;

//5.1.2 transform
	vector<int> v2;				//创建目标容器
	cout << v2.size() << endl; //初始的容器大小为0,所以↓
	v2.resize(v1.size());		//目标容器需要重新开辟空间
	transform(v1.begin(), v1.end(), v2.begin(), MyTransform());//把容器1的内容搬运过来
	for_each(v2.begin(), v2.end(), MyPrint());//
	cout << endl;

	system("pause");
	return 0;
}

5.2 常用查找算法

5.2.1 find //查找元素
5.2.2 find_if //按条件查找元素;4.2.2一元谓词用过
5.2.3 adjacent_find //查找相邻重复元素
5.2.4 binary_search //二分查找法
5.2.5 count //统计元素个数
5.2.6 count_if //按条件统计元素个数

5.2.1 & 5.2.2 & 5.2.3:返回值都是迭代器

find(iterator beg, iterator end, value); //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置;—返回的是迭代器
find_if(iterator beg, iterator end, _Pred); // 按条件查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置;// _Pred 函数或者谓词(返回bool类型的仿函数)—返回的是迭代器
adjacent_find(iterator beg, iterator end); // 查找相邻重复元素,返回相邻元素的第一个位置的迭代器—返回的是迭代器

**下面的示例中几个注意点:
1.①和②在查找自定义数据类型(Person类)时候,需要在Person类里重载 operator==运算符;
2.设置一个标志位flag,如果已经找到了,就置真,下次在循环的时候就不再输出“没找到”;
3.直接查找自定义数据类型p6,返回值用一个迭代器来接收;
4和5一样:由于有可能找到不止一个满足条件的元素,所以就要遍历整个容器,每找到一个元素就输出,下次再查找的时候就从这个元素开始接着找。(具体看程序)
**

示例(5.2.1 & 5.2.2 & 5.2.3):

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;
//自定义数据类型:
class Person {
public:
	string name;
	int age;
	int height;

	Person(string name, int age, int height) {
		this->name = name;
		this->age = age;
		this->height = height;
	}
	//1.重载相等==运算符
	bool operator==(const Person& p1) {//记得加const
		if (this->name == p1.name && this->age == p1.age && this->height == p1.height)
			return 1;
		else
			return 0;
	}
};
//5.2.2 find_if
class FindFive {
public:
	bool operator()(int val) {
		if (val == 5)//条件:查找等于5的元素
			return 1;
		else
			return 0;
	}
};

class FindPi {
public:
	bool operator()(const Person& p) {
		if (p.age > 26)//条件:查找大于26岁的人
		//if(p.name == "Reus" && p.age == 26  && p.height == 185)
			return 1;
		else
			return 0;
	}
};


int main() {

	//2设置标志位flag
	bool flag = false;//是否找到
	vector<int> v1;
	for (int i = 0; i < 5; i++) {
		v1.push_back(i + 1);//1 2 3 4 5
	}

	Person p1("Tom", 23, 175);
	Person p2("Jerry", 25, 163);//26
	Person p3("Reus", 28, 185);
	Person p4("Lucas", 29, 177);//26
	Person p5("Messi", 26, 173);

	vector<Person> v2;
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);

//5.2.1 find
	//①容器中存放内置数据类型:
	cout << "find(int 3):" ;
	vector<int>::iterator it = find(v1.begin(), v1.end(), 3);
	if (it == v1.end())
		cout << "没找到!!!" << endl;
	else
		cout << "找到了!" << endl;//√
	
	cout << "find(int 6):";
	vector<int>::iterator it1 = find(v1.begin(), v1.end(), 6);
	if (it1 == v1.end())
		cout << "没找到!!!" << endl;//√
	else
		cout << "找到了!" << endl;
	//②容器中存放自定义数据类型:
	Person p6("Jerry", 26, 163);//26:没找到  25:找到了
	cout << "find(Person p6):";
	//3
	vector<Person>::iterator it3 = find(v2.begin(), v2.end(), p6);
	if (it3 == v2.end())
		cout << "没找到!!!" << endl;//√
	else
		cout << "找到了!,姓名:" << it3->name << "\t年龄:" << it3->age << endl;


//5.2.2 find_if
	//①容器中存放内置数据类型:
	cout << "find_if(等于5的数):";
	vector<int>::iterator it6 = find_if(v1.begin(), v1.end(), FindFive());//谓词FindFive
	if (it6 == v1.end())
		cout << "没找到!!!" << endl;//√
	else
		cout << "找到了!" << endl;
	//②容器中存放自定义数据类型:
	cout << "find_if(大于26岁的元素):";
	//4
	for(vector<Person>::iterator it = v2.begin();it != v2.end();it++){
	
		vector<Person>::iterator it7 = find_if(it, v2.end(), FindPi());
		if (it7 == v2.end()) {
			if(!flag)
				cout << "没找到!!!" << endl;//√
			break;
		}	
		else {
			cout << "找到了:姓名:" << it7->name << "\t年龄:" << it7->age << endl;
			it = it7;
			flag = true;
		}
			
	}

//5.2.3 adjacent_find
	v1.push_back(6);
	v1.push_back(6);
	v1.push_back(7);
	v1.push_back(7);
	v1.push_back(8);
	cout << "adjacent_find(相邻重复元素):";
	//5
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
		vector<int>::iterator it10 = adjacent_find(it, v1.end());
		if (it10 == v1.end()) {
			if(!flag)
				cout << "没找到!!!" << endl;
			break;
		}
		else {
			cout << "找到了!对应元素为" << *it10 << endl;
			it = it10;
			flag = true;
		}
			
	}

	system("pause");
	return 0;
}

5.2.4 & 5.2.5 & 5.2.6 (二分法查找&统计)

bool binary_search(iterator beg, iterator end, value); //二分查找法:查找指定的元素,查到 返回true 否则false—返回值是bool类型
count(iterator beg, iterator end, value); // 统计元素出现次数—返回值是int
count_if(iterator beg, iterator end, _Pred); // 按条件统计元素出现次数; // _Pred 谓词—返回值是int
注意:
1.二分查找法查找效率很高,但前提是查找的容器中元素必须有序,在无序序列中不可用
2&3.在统计自定义数据类型(Person类)时候,需要在Person类内重载 operator==运算符。

示例:

//5.2.4 & 5.2.5 & 5.2.6
/**/
//自定义数据类型:
class Person {
public:
	string name;
	int age;
	int height;

	Person(string name, int age, int height) {
		this->name = name;
		this->age = age;
		this->height = height;
	}
	//重载等于==运算符
	bool operator==(const Person& p) {
		if (this->name == p.name && this->age == p.age && this->height == p.height)
			return 1;
		else
			return 0;
	}

};

class FindGreater3 {
public:

	bool operator()( int val) {
		if (val >= 3)//条件:找≥3的元素
			return 1;
		else
			return 0;
	}
};
class FindPerson {
public:
	bool operator()(const Person& p) {
		if (p.age > 26)//条件:找>26岁的人
			return 1;
		else
			return 0;
	}
};
int main(){

	vector<int> v3;
	for (int i = 0; i < 5; i++) {
		v3.push_back(i + 1);
	}

	Person p1("Tom", 23, 175);
	Person p2("Jerry", 25, 163);//26
	Person p3("Reus", 28, 185);
	Person p4("Lucas", 29, 177);//26
	Person p5("Messi", 26, 173);

	vector<Person> v4;
	v4.push_back(p1);
	v4.push_back(p2);
	v4.push_back(p3);
	v4.push_back(p4);
	v4.push_back(p5);

//5.2.4 binary_search
	cout << "binary_search(找int 3):";
	if (binary_search(v3.begin(), v3.end(), 3))//返回值是bool类型
		cout << "找到了" << endl;
	else
		cout << "没找到!!!" << endl;

//5.2.5 count
	//内置数据类型:
	v3.push_back(3);
	v3.push_back(2);
	v3.push_back(3);
	cout << "元素2的个数:" << count(v3.begin(), v3.end(), 2) << endl;//2
	cout << "元素3的个数:" << count(v3.begin(), v3.end(), 3) << endl;//3

	//自定义数据类型:
	Person p6("Reus", 28, 185);
	Person p7("Reus", 28, 185);
	Person p8("Reus", 27, 185);
	v4.push_back(p6);
	v4.push_back(p7);
	//v4.push_back(p8);
	cout << "元素p6的个数:" << count(v4.begin(), v4.end(), p6) << endl;//3
	cout << "元素p5的个数:" << count(v4.begin(), v4.end(), p5) << endl;//1
	cout << "元素p8的个数:" << count(v4.begin(), v4.end(), p8) << endl;//0

//5.2.6 count_if
	//内置数据类型:
	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;//1 2 3 4 5 3 2 3
		
	cout << ">=3的元素个数:" << count_if(v3.begin(), v3.end(), FindGreater3()) << endl;//5
		
	//自定义数据类型:
	for (vector<Person>::iterator it = v4.begin(); it != v4.end(); it ++ ) {
		cout << "姓名:" << it->name << "\t年龄:" << it->age << "\t身高:" << it->height << endl;
	}//结果如下:
	//姓名:Tom       年龄:23        身高:175
	//姓名:Jerry     年龄:25        身高:163
	//姓名:Reus      年龄:28        身高:185
	//姓名:Lucas     年龄:29        身高:177
	//姓名:Messi     年龄:26        身高:173
	//姓名:Reus      年龄:28        身高:185
	//姓名:Reus      年龄:28        身高:185
	cout << "年龄大于26岁的人数:" << count_if(v4.begin(), v4.end(), FindPerson()) << endl;//4

	system("pause");
	return 0;
}

5.3 常用排序算法

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

5.3.1 sort
sort(iterator beg, iterator end, _Pred);//对容器内元素进行排序

5.3.2 random_shuffle(洗牌)
random_shuffle(iterator beg, iterator end); // 指定范围内的元素随机调整次序

5.3.4 merge
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); // 容器元素合并,并存储到另一容器
注意: 1.两个容器必须是有序的;2.目标容器需要重新开辟空间
// beg1 容器1开始迭代器 // end1 容器1结束迭代器
// beg2 容器2开始迭代器 // end2 容器2结束迭代器
// dest 目标容器开始迭代器

5.3.4 reverse
reverse(iterator beg, iterator end); //将容器内元素进行反转

示例:

//5.3 常用排序算法:
//打印vector<int> v
void printVectors(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
}
//利用仿函数自定义排序规则:降序
class MySort1 {
public :
	bool operator()(int a,int b) {
		return a > b;
	}
};

int main() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(60);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50); 
	v1.push_back(20);
	printVectors(v1);//

//5.3.1 sort
	//默认排序规则:升序
	sort(v1.begin(), v1.end());
	printVectors(v1);//10 20 30 40 50 60 
	//利用仿函数自定义排序规则:降序
	sort(v1.begin(), v1.end(),MySort1());
	printVectors(v1);//60 50 40 30 20 10
	//直接用内建仿函数(大于仿函数greater<>()):降序
	sort(v1.begin(), v1.end(),greater<>());
	printVectors(v1);//60 50 40 30 20 10

//5.3.2 random_shuffle(洗牌)
	//随机数种子:
	srand((unsigned int)time(NULL));//每次编译运行后的结果都不一样
	random_shuffle(v1.begin(), v1.end());
	printVectors(v1);//20 50 30 40 60 10;10 50 20 60 40 30;60 40 30 50 20 10

//5.3.3 merge(v2和v3要求有序,且v4要重新开辟空间)
	vector<int> v2;
	v2.push_back(6);
	v2.push_back(9);
	v2.push_back(31);

	vector<int> v3;
	v3.push_back(10);
	v3.push_back(26);
	v3.push_back(50);

	vector<int> v4;
	v4.resize(v2.size() + v3.size());
	merge(v2.begin(), v2.end(), v3.begin(), v3.end(), v4.begin());
	printVectors(v4);//6 9 10 26 31 50

//5.3.4 reverse
	printVectors(v1);//50 20 30 10 40 60
	reverse(v1.begin(),v1.end());
	printVectors(v1);//60 40 10 30 20 50

	printVectors(v4);//6 9 10 26 31 50
	reverse(v4.begin(), v4.end());
	printVectors(v4);//50 31 26 10 9 6

	system("pause");
	return 0;
}

5.4 常用拷贝和替换算法

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

5.4.1 copy
copy(iterator beg, iterator end, iterator dest); //容器内指定范围的元素拷贝到另一容器中

5.4.2 replace
replace(iterator beg, iterator end, oldvalue, newvalue); //将容器内指定范围的旧元素修改为新元素
// oldvalue 旧元素 // newvalue 新元素

5.4.3 replace_if
replace_if(iterator beg, iterator end, _pred, newvalue); // 按条件替换元素,满足条件的替换成指定元素
// _pred 谓词 // newvalue 替换的新元素

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

示例:

//5.4 常用拷贝和替换算法:
//打印vector<int> v
void printVectors(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
}
//5.4.3 replace_if
class FindVec {
public:
	bool operator()(int val) {//谓词
		if (val > 50)//条件:大于50的元素
			return 1;
		else
			return 0;
	}
};
int main() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(60);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	v1.push_back(20);
	printVectors(v1);//10 60 30 40 50 20

//5.4.1 copy 
	vector<int> v2;
	v2.resize(v1.size());//重新开辟空间
	copy(v1.begin(), v1.end(), v2.begin());
	printVectors(v2);//10 60 30 40 50 20

//5.4.2 replace
	replace(v2.begin(), v2.end(), 30, 26);
	printVectors(v2);//10 60 26 40 50 20
	v2.push_back(40);
	replace(v2.begin(), v2.end(), 40, 26);
	printVectors(v2);//10 60 26 26 50 20 26
	replace(v2.begin(), v2.end(), 25, 26);//容器中没有25,就不替换了
	printVectors(v2);//10 60 26 26 50 20 26

//5.4.3 replace_if
	vector<int> v3;
	v3.resize(v2.size());
	copy(v2.begin(), v2.end(), v3.begin());
	printVectors(v3);//10 60 26 26 50 20 26
	replace_if(v3.begin(), v3.end(), FindVec(), 100);
	printVectors(v3);//10 100 26 26 50 20 26

//5.4.4 swap
	vector<int> v4;
	for (int i = 0; i < 5; i++) {
		v4.push_back(i + 1);
	}
	swap(v3, v4);
	printVectors(v3);//1 2 3 4 5
	printVectors(v4);//10 100 26 26 50 20 26

	system("pause");
	return 0;
}

5.5 常用算数生成算法

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

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

5.5.1 accumulate(记得加头文件#include <numeric>
accumulate(iterator beg, iterator end, value); // 计算容器元素累计总和—返回值为int
// beg:开始迭代器 // end:结束迭代器 // value:起始值
注意:最后的总和把value值也加进去了
补充:见生成double型随机数 & accumulate的精确度

5.5.2 fill
fill(iterator beg, iterator end, value); // 将容器区间内元素填充为指定值
// beg 开始迭代器 // end 结束迭代器 // value:要填充的值

示例:

#include<iostream>
#include<numeric>
#include<vector>

using namespace std;

//打印vector<int> v
void printVectors(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
}

int main() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(60);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	v1.push_back(20);
	printVectors(v1);//10 60 30 40 50 20

//5.5.1 accumulate
	cout << "总和(初始值为1):" << accumulate(v1.begin(), v1.end(), 1) << endl;//111
	cout << "总和(初始值为10):" << accumulate(v1.begin(), v1.end(), 10) << endl;//120

//5.5.2 fill
	printVectors(v1);//10 60 30 40 50 20
	fill(v1.begin(), v1.end(), 26);
	printVectors(v1);//26 26 26 26 26 26

	system("pause");
	return 0;
}

5.6 常用集合算法

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

5.6.1 set_intersection
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); // 求两个集合的交集
5.6.2 set_union
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); // 求两个集合的并集
5.6.3 set_difference
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest); // 求两个集合的差集

5.6.1& 5.6.2 & 5.6.3注意:
相同点:
1.两个集合必须是有序序列
2.返回值是迭代器,所以用一个迭代器接收求两个容器的交/并集后的返回值,作为打印函数的结束迭代器
不同点:
交集后新容器的大小为v3.resize(min(v1.size(),v2.size()));
并集后新容器的大小为v4.resize(v1.size() + v2.size());
差集后新容器的大小为v5.resize(max(v1.size(),v2.size()));

示例:
①为新容器重新开辟空间;
②返回目标容器的最后一个元素的迭代器地址;
③将目标容器的最后一个元素的迭代器地址作为打印函数的结束迭代器
④用
for_each(v1.begin(), v1.end(), MyPrint);//自定义打印函数

for_each(v2.begin(), v2.end(), MyPrint1());//利用仿函数,实现打印功能
打印容器

//自定义打印函数:
void MyPrint(int val) {

	cout << val << " ";
}
//利用仿函数,实现打印功能:
class MyPrint1 {
public:
	void operator()(int val) {//返回void,不是返回bool
		cout << val << " ";
	}
};

int main() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(60);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	v1.push_back(20);
	
	sort(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), MyPrint);//10 20 30 40 50 60
	cout << endl;

	vector<int> v2;
	v2.push_back(50);
	v2.push_back(60);
	v2.push_back(70);

//5.6.1	set_intersection
	int min0 = (v1.size() > v2.size()) ? v2.size() : v1.size();
	int min1 = min(v1.size(), v2.size());
	vector<int> v3;
	//v3.resize(min0);
	//v3.resize(min1);
	v3.resize(min(v1.size(),v2.size()));//①取两个里面较小的值给目标容器重新开辟空间
	vector<int>::iterator itera = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());//②返回目标容器的最后一个元素的迭代器地址
	for_each(v3.begin(), itera, MyPrint1());//50 60 ③将目标容器的最后一个元素的迭代器地址作为打印函数的结束迭代器
	cout << endl;

//5.6.2 set_union
	vector<int> v4;
	v4.resize(v1.size() + v2.size());//①取两个容器的和给目标容器重新开辟空间
	vector<int>::iterator itera1 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin());//②返回目标容器的最后一个元素的迭代器地址
	for_each(v4.begin(), itera1, MyPrint1());//10 20 30 40 50 60 70 ③将目标容器的最后一个元素的迭代器地址作为打印函数的结束迭代器
	cout << endl;

//5.6.3 set_difference
	vector<int> v5;
	v5.resize(max(v1.size(), v2.size()));//①
	//容器v1与v2的差集:
	cout << "容器v1与v2的差集:";
	vector<int>::iterator itera2 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v5.begin());//②
	for_each(v5.begin(), itera2, MyPrint1());//10 20 30 40 ③
	cout << endl;
	//容器v2与v1的差集:
	cout << "容器v2与v1的差集:";
	vector<int>::iterator itera3 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v5.begin());//②
	for_each(v5.begin(), itera3, MyPrint1());//70 ③
	cout << endl;
	
	system("pause");
	return 0;
}

2021.9.8 11:50 C++提高编程结束
接下来进行第六和第七阶段

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值