C++基础学习笔记(八)——提高编程PART3

news2024/10/6 22:31:59

参考链接:https://www.bilibili.com/video/BV1et411b73Z/p=237&spm_id_from=pageDriver&vd_source=b4d9cee68649c8adcb1e266f7147cd5c

4 STL- 函数对象

4.1 函数对象

4.1.1 函数对象概念

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

本质:
函数对象(仿函数)是一个类,不是一个函数

4.1.2 函数对象使用

特点:
函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
函数对象超出普通函数的概念,函数对象可以有自己的状态
函数对象可以作为参数传递

demo65:

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

//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test01()
{
	MyAdd myAdd;
	cout << myAdd(10, 10) << endl;
}

//2、函数对象可以有自己的状态
class MyPrint
{
public:
	MyPrint()
	{
		count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		count++; //统计使用次数
	}
	int count; //内部自己的状态
};
void test02()
{
	MyPrint myPrint;
	myPrint("hello world");
	myPrint("hello world");
	myPrint("hello world");
	cout << "myPrint调用次数为: " << myPrint.count << endl;
}

//3、函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{
	mp(test);
}
void test03()
{
	MyPrint myPrint;
	doPrint(myPrint, "Hello C++");
}

int main()
{
	test01();
	test02();
	test03();

	system("pause");
	return 0;
}
20
hello world
hello world
hello world
myPrint调用次数为: 3
Hello C++

总结:
仿函数写法非常灵活,可以作为参数进行传递。

4.2 谓词

4.2.1 谓词概念

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

4.2.2 一元谓词

demo66:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

//1.一元谓词
struct GreaterFive {
	bool operator()(int val) {
		return val > 5;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//查找容器中 有没有大于5的数字
	//GreaterFive() 匿名函数对象
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没找到!" << endl;
	}
	else {
		cout << "找到:" << *it << endl;
	}
}

int main()
{
	test01();

	system("pause");
	return 0;
}
找到:6

总结:参数只有一个的谓词,称为一元谓词

4.2.3 二元谓词

demo67:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

//二元谓词
class MyCompare
{
public:
	bool operator()(int num1, int num2)
	{
		return num1 > num2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	//默认从小到大
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	cout << "----------------------------" << endl;

	//使用函数对象改变算法策略,排序从大到小
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
10 20 30 40 50
----------------------------
50 40 30 20 10

总结:参数只有两个的谓词,称为二元谓词

4.3 内建函数对象

4.3.1 内建函数对象意义

概念:
STL内建了一些函数对象

分类:
算术仿函数
关系仿函数
逻辑仿函数

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

4.3.2 算术仿函数

功能描述:
实现四则运算
其中negate是一元运算,其他都是二元运算

仿函数原型:
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> //取反仿函数

demo68:

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

//negate
void test01()
{
	negate<int> n;
	cout << n(50) << endl;
}
//plus
void test02()
{
	plus<int> p;
	cout << p(10, 20) << endl;
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}
-50
30

总结:使用内建函数对象时,需要引入头文件 #include <functional>

4.3.3 关系仿函数

功能描述:
实现关系对比

仿函数原型:
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> //小于等于

demo69:

#include<iostream>
#include<string> 
using namespace std;
#include <functional>
#include <vector>
#include <algorithm>

//大于 greater
class MyCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(40);
	v.push_back(20);
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	//自己实现仿函数
	//sort(v.begin(), v.end(), MyCompare());
	
	//STL内建仿函数 大于仿函数
	sort(v.begin(), v.end(), greater<int>());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
10 30 50 40 20
50 40 30 20 10

总结:关系仿函数中最常用的就是greater<>大于

4.3.4 逻辑仿函数

功能描述:
实现逻辑运算

函数原型:
template<class T> bool logical_and<T> //逻辑与
template<class T> bool logical_or<T> //逻辑或
template<class T> bool logical_not<T> //逻辑非

demo70:

#include<iostream>
#include<string> 
using namespace std;
#include <functional>
#include <vector>
#include <algorithm>


void test01()
{
	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;

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

int main()
{
	test01();

	system("pause");
	return 0;
}
1 0 1 0
0 1 0 1

总结:逻辑仿函数实际应用较少,了解即可

5 STL- 常用算法

概述:
算法主要是由头文件<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 函数或者函数对象

demo71:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>


//普通函数
void print01(int val)
{
	cout << val << " ";
}
//函数对象
class print02
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
//for_each算法基本用法
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

总结:for_each在实际开发中是最常用遍历算法,需要熟练掌握

5.1.2 transform

功能描述:
搬运容器到另一个容器中

函数原型:

transform(iterator beg1, iterator end1, iterator beg2, _func);

//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象

demo72:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>


//常用遍历算法 搬运 transform
class TransForm
{
public:
	int operator()(int val)
	{
		return val;
	}
};
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());
}

int main()
{
	test01();

	system("pause");
	return 0;
}
0 1 2 3 4 5 6 7 8 9

总结: 搬运的目标容器必须要提前开辟空间,否则无法正常搬运

5.2 常用查找算法

学习目标:
掌握常用的查找算法

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

5.2.1 find

功能描述:
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

函数原型:

find(iterator beg, iterator end, value);

// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素

demo73:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

//查找 内置数据类型
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}
	//查找容器中是否有 5 这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	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;
		}
		return false;
	}
public:
	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;
}
找到:5
找到姓名:bbb 年龄: 20

总结: 利用find可以在容器中找指定的元素,返回值是迭代器

5.2.2 find_if

功能描述:
按条件查找元素

函数原型:

find_if(iterator beg, iterator end, _Pred);

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

demo74:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

//内置数据类型
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 + 1);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到大于5的数字:" << *it << endl;
	}
}


//自定义数据类型
class Person {
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	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);

	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
	if (it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
	}
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

找到大于5的数字:6
找到姓名:ccc 年龄: 30

总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略

5.2.3 adjacent_find

功能描述:
查找相邻重复元素

函数原型:

adjacent_find(iterator beg, iterator end);

// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
// beg 开始迭代器
// end 结束迭代器

demo75:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(4);
	v.push_back(3);
	//查找相邻重复元素
	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end()) {
		cout << "找不到!" << endl;
	}
	else {
		cout << "找到相邻重复元素为:" << *it << endl;
	}
}


int main()
{
	test01();

	system("pause");
	return 0;
}
找到相邻重复元素为:4

总结:面试题中如果出现查找相邻重复元素,记得用STL中的adjacent_find算法

5.2.4 binary_search

功能描述:
查找指定元素是否存在

函数原型:

bool binary_search(iterator beg, iterator end, value);

// 查找指定的元素,查到 返回true 否则false
// 注意: 在无序序列中不可用
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素

demo76:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//二分查找
	//注意:容器必须是有序的序列(升序)
	//如果是无序序列,结果未知
	bool ret = binary_search(v.begin(), v.end(), 2);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}


int main()
{
	test01();

	system("pause");
	return 0;
}
找到了

总结:二分查找法查找效率很高,值得注意的是查找的容器中元素必须的有序序列

5.2.5 count

功能描述:
统计元素个数

函数原型:

count(iterator beg, iterator end, value);

// 统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// value 统计的元素

demo77:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(30);
	v.push_back(40);
	v.push_back(40);
	int num = count(v.begin(), v.end(), 40);
	cout << "40的个数为: " << num << endl;
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->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 test02()
{
	vector<Person> v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("诸葛亮", 35);
	int num = count(v.begin(), v.end(), p);
	cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

40的个数为: 3
和诸葛亮同岁数的人员个数为:3

总结: 统计自定义数据类型时候,需要配合重载 operator==

5.2.6 count_if

功能描述:
按条件统计元素个数

函数原型:

count_if(iterator beg, iterator end, _Pred);

// 按条件统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词

demo78:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

class Greater4
{
public:
	bool operator()(int val)
	{
		return val >= 4;
	}
};
//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);
	int num = count_if(v.begin(), v.end(), Greater4());
	cout << "大于等于4的个数为: " << num << endl;
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

class AgeLess35
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age < 35;
	}
};
void test02()
{
	vector<Person> v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);
	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(), AgeLess35());
	cout << "小于35岁的个数:" << num << endl;
}


int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}
大于等于4的个数为: 4
小于35岁的个数:2

总结:按值统计用count,按条件统计用count_if

5.3 常用排序算法

学习目标:
掌握常用的排序算法

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

5.3.1 sort

功能描述:
对容器内元素进行排序

函数原型:

sort(iterator beg, iterator end, _Pred);

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

demo79:

#include<iostream>
#include<string> 
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;

	//从大到小排序
	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

总结:sort属于开发中最常用的算法之一,需熟练掌握

5.3.2 random_shuffle

功能描述:
洗牌 指定范围内的元素随机调整次序

函数原型:

random_shuffle(iterator beg, iterator end);

// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器

demo80:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>
#include<ctime>

class myPrint
{
public:
	void operator()(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);
	}
	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();

	system("pause");
	return 0;
}
0 1 2 3 4 5 6 7 8 9
8 4 1 0 6 2 3 7 9 5

总结:random_shuffle洗牌算法比较实用,使用时记得加随机数种子

5.3.3 merge

功能描述:
两个容器元素合并,并存储到另一容器中

函数原型:

merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

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

demo81:

#include<iostream>
#include<string> 
using namespace std;
#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 + 1);
	}
	//目标容器
	vector<int> vtarget;
	vtarget.resize(v1.size() + v2.size()); //目标容器需要提前开辟空间
	//合并 需要两个有序序列,而且顺序一致,比如均为升序
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
	for_each(vtarget.begin(), vtarget.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

总结:merge合并的两个容器必须的有序序列

5.3.4 reverse

功能描述:
将容器内元素进行反转

函数原型:

reverse(iterator beg, iterator end);

// 反转指定范围的元素
// beg 开始迭代器
// end 结束迭代器

demo82:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

class myPrint
{
public:
	void operator()(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 << "反转前: " << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	cout << "反转后: " << endl;
	reverse(v.begin(), v.end());
	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

总结:reverse反转区间内元素,面试题可能涉及到

5.4 常用拷贝和替换算法

学习目标:
掌握常用的拷贝和替换算法

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

5.4.1 copy

功能描述:
容器内指定范围的元素拷贝到另一容器中

函数原型:

copy(iterator beg, iterator end, iterator dest);

// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// dest 目标起始迭代器

demo83:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i + 1);
	}
	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;
}
1 2 3 4 5 6 7 8 9 10

总结:利用copy算法在拷贝时,目标容器记得提前开辟空间

5.4.2 replace

功能描述:
将容器内指定范围的旧元素修改为新元素

函数原型:

replace(iterator beg, iterator end, oldvalue, newvalue);

// 将区间内旧元素 替换成 新元素
// beg 开始迭代器
// end 结束迭代器
// oldvalue 旧元素
// newvalue 新元素

demo84:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(10);
	v.push_back(20);

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//将容器中的20 替换成 2000
	cout << "替换后:" << endl;
	replace(v.begin(), v.end(), 20, 2000);
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
替换前:
20 30 20 40 50 10 20
替换后:
2000 30 2000 40 50 10 2000

总结:replace会替换区间内满足条件的元素

5.4.3 replace_if

功能描述:
将区间内满足条件的元素,替换成指定元素

函数原型:

replace_if(iterator beg, iterator end, _pred, newvalue);

// 按条件替换元素,满足条件的替换成指定元素
// beg 开始迭代器
// end 结束迭代器
// _pred 谓词
// newvalue 替换的新元素

demo85:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <algorithm>

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

class ReplaceGreater30
{
public:
	bool operator()(int val)
	{
		return val >= 30;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(10);
	v.push_back(20);
	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//将容器中大于等于的30 替换成 3000
	cout << "替换后:" << endl;
	replace_if(v.begin(), v.end(), ReplaceGreater30(), 3000);
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
替换前:
20 30 20 40 50 10 20
替换后:
20 3000 20 3000 3000 10 20

总结:replace_if按条件查找,可以利用仿函数灵活筛选满足的条件

5.4.4 swap

功能描述:
互换两个容器的元素

函数原型:

swap(container c1, container c2);

// 互换两个容器的元素
// c1容器1
// c2容器2

demo86:

#include<iostream>
#include<string> 
using namespace std;
#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());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;

	cout << "-------------------" << endl;
	cout << "交换后: " << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	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
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

总结:swap交换容器时,注意交换的容器要同种类型

5.5 常用算术生成算法

学习目标:
掌握常用的算术生成算法

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

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

5.5.1 accumulate

功能描述:
计算区间内 容器元素累计总和

函数原型:

accumulate(iterator beg, iterator end, value);

// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 起始值

demo87:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <numeric>


void test01()
{
	vector<int> v;
	for (int i = 0; i <= 100; i++) {
		v.push_back(i);
	}
	//参数3  是起始累加值;如果改成1000,结果为6050
	int total = accumulate(v.begin(), v.end(), 0);
	cout << "total = " << total << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

total = 5050

总结:accumulate使用时头文件注意是 numeric,这个算法很实用

5.5.2 fill

功能描述:
向容器中填充指定的元素

函数原型:

fill(iterator beg, iterator end, value);

// 向容器中填充元素
// beg 开始迭代器
// end 结束迭代器
// value 填充的值

demo88:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <numeric>
#include <algorithm>

class myPrint
{
public:
	void operator()(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

总结:利用fill可以将容器区间内元素填充为 指定的值

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);

// 求两个集合的交集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器 // end1 容器1结束迭代器 // beg2 容器2开始迭代器 // end2 容器2结束迭代器 //
dest 目标容器开始迭代器

demo89:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <numeric>
#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 + 5);
	}
	vector<int> vTarget;
	//取两个里面较小的值给目标容器开辟空间
	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());
	cout << endl;
	cout << "如果用vTarget.end(),会返回整体的长度(其他用0 填充)"<< endl;
	for_each(vTarget.begin(), vTarget.end(),myPrint());
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
5 6 7 8 9
如果用vTarget.end(),会返回整体的长度(其他用0 填充)
5 6 7 8 9 0 0 0 0 0

总结:
求交集的两个集合必须的有序序列
目标容器开辟空间需要从两个容器中取小值
set_intersection返回值既是交集中最后一个元素的位置

5.6.2 set_union

功能描述:
求两个集合的并集

函数原型:

set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// 求两个集合的并集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器 // end1 容器1结束迭代器 // beg2 容器2开始迭代器 // end2 容器2结束迭代器 //
dest 目标容器开始迭代器

demo90:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <numeric>
#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 + 5);
	}
	vector<int> vTarget;
	//取两个容器的和给目标容器开辟空间
	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(), 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

总结:
求并集的两个集合必须的有序序列
目标容器开辟空间需要两个容器相加
set_union返回值既是并集中最后一个元素的位置

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 目标容器开始迭代器

在这里插入图片描述

demo91:

#include<iostream>
#include<string> 
using namespace std;
#include <vector>
#include <numeric>
#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 + 5);
	}
	vector<int> vTarget;
	//取两个里面较大的值给目标容器开辟空间
	vTarget.resize(max(v1.size(), v2.size())); //最特殊情况:两容器无交集

	//返回目标容器的最后一个元素的迭代器地址
	cout << "v1与v2的差集为: " << endl;
	vector<int>::iterator itEnd =
		set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	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());
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
v1与v2的差集为:
0 1 2 3 4
v2与v1的差集为:
10 11 12 13 14

总结:
求差集的两个集合必须的有序序列
目标容器开辟空间需要从两个容器取较大值
set_difference返回值既是差集中最后一个元素的位置

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/418820.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STL的并行遍历:for_each(依赖TBB)和omp parallel

文章目录OMP parallelOpenMP安装OpenMP示例1) OMP Hello World2) OMP for 并行3. OMP 官方示例4) map使用OMP遍历TBB的安装和使用Gcc9的安装TBB 安装TBB使用在图像处理等应用中&#xff0c;我们经常需要对矩阵&#xff0c;大数量STL对象进行遍历操作&#xff0c;因此并行化对算…

R语言与作物模型(以DSSAT模型为例)融合应用

随着基于过程的作物生长模型&#xff08;Process-based Crop Growth Simulation Model&#xff09;的发展&#xff0c;R语言在作物生长模型和数据分析、挖掘和可视化中发挥着越来越重要的作用。想要成为一名优秀的作物模型使用者与科研团队不可或缺的人才&#xff0c;除了掌握对…

MySQL存储过程 if、case、while、loop、游标、变量、条件处理程序

存储过程是事先经过编译并存储在数据库中的一段 SQL 语句的集合&#xff0c;调用存储过程可以简化很多工作&#xff0c;减少数据在数据库和应用服务器之间的传输&#xff0c;对于提高数据处理的效率是有好处的。 存储过程思想上很简单&#xff0c;就是数据库 SQL 语言层面的代…

barret reduction原理详解及硬件优化

背景介绍 约减算法&#xff0c;通常应用在硬件领域&#xff0c;因为模运算mod是一个除法运算&#xff0c;在硬件中实现速度会比乘法慢的多&#xff0c;并且还会占用大量资源&#xff0c;因此需要想办法用乘法及其它简单运算来替代模运算。模约减算法可以利用乘法、加法和移位等…

怎么评价2023年第十三届MathorCup高校数学建模挑战赛?

文章目录赛题思路选题建议1 竞赛信息2 竞赛时间3 组织机构4 建模常见问题类型4.1 分类问题4.2 优化问题4.3 预测问题4.4 评价问题赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; 选题建议 首先要注意&#xff0c;A、B题为研究生组可选题目&#xff0c;A…

还在用 if else 做参数校验?快来学习高级参数校验吧

文章目录一、前言二、自定义校验2.1 定义 GenderArrayValuable 接口2.2 定义性别 GenderEnum 枚举类2.3 自定义 GenderCheck 自定义约束注解2.4 自定义约束的校验器 GenderValidator2.5 定义 UserUpdateGenderDTO2.6 定义一个对外访问接口2.7 请求接口 进行验证三、总结一、前言…

从C出发 17 --- 函数调用

从表面上来看&#xff0c;函数就是一个代码片段&#xff0c;只不过说这个代码片段可以反复利用&#xff0c;通过调用的方式反复利用&#xff0c;通过函数调用&#xff0c;我们可以将参数传到函数所对应的代码片段里面&#xff0c;然后代码片段去处理这些参数&#xff0c;得到一…

Linux下Samba服务器的安装与配置(简单实用)

为了可以实现Linux与windows之间实现文件的共享&#xff0c;方便文件可以直接修改&#xff0c;而不是像以前需要拷贝文件再进行修改&#xff0c;samba的诞生是为了实现现在的这些需求。我们知道Linux之间可以使用NFS服务器来实现文件的共享&#xff0c;samba的诞生就是为了使wi…

Spring Cloud Security

Spring Cloud Security Spring Cloud Security用于构建微服务的安全应用程序和服务&#xff0c;它可以轻松实现微服务系统架构下的统一安全认证与授权。 Spring Cloud Security 有以下组件。 spring-cloud-security&#xff1a;为Zuul、Feign、Oauth 2.0 的Resource Serve…

ChatGPT5是否会影响人类的发展和工作?

目录前言ChatGPT5是什么ChatGPT5 的潜在影响挑战与风险总结前言 ChatGPT的普及也带来了大量的讨论&#xff0c;关于它是否会影响人类的发展和工作。本文将讨论 ChatGPT5 如何可能改变人类的工作和发展&#xff0c;以及潜在的利弊和挑战。在话题开始之前&#xff0c;让我们先从…

QxOrm的使用-数据操作--增删改查

文章目录QxOrm的使用-数据操作使用QxOrm对数据库进行增删改查新增数据删除数据修改数据查询数据QxOrm的使用-数据操作 上一篇我们讲了QxOrm的基本的数据映射操作&#xff0c;这里面再补充一点东西 数据类型映射 Qt/C类型数据库类型boolSMALLINTqx_boolSMALLINTshortSMALLINT…

【谷粒商城之整合阿里云OSS对象存储】

本笔记内容为尚硅谷谷粒商城整合阿里云OSS对象存储部分 目录 一 、简介 二、云存储开通与使用 1、开通阿里云对象存储服务 2、创建bucket 3、创建子用户&#xff08;获取密钥访问OSS服务器&#xff09; 给该子账户添加权限 4、阿里云对象存储上传方式 三、整合 1、…

BUUCTF--Web篇详细wp

BUUCTF--Web篇详细wp[极客大挑战 2019]EasySQL[极客大挑战 2019]Havefun[HCTF 2018]WarmUp[ACTF2020 新生赛]Include[ACTF2020 新生赛]Exec[强网杯 2019]随便注[GXYCTF2019]Ping Ping Ping[SUCTF 2019]EasySQL[极客大挑战 2019]Secret File[极客大挑战 2019]LoveSQL[极客大挑战…

MySQL 分布式数据库实现:无需修改代码,轻松实现分布式能力

这个项目做什么 ShardingSphere-Proxy&#xff0c;可以让用户像使用原生数据库一样使用 Apache ShardingSphere。 了解一项技术的开始&#xff0c;一般从官网开始。先来看一看官网对 ShardingSphere-Proxy 的定义是什么样的&#xff1a; 定位为透明化的数据库代理端&#xff…

异配图神经网络——Graph Transformer Networks

一.论文概述 作者提出了Graph Transformer Network (GTN)用来在异配图&#xff08;heterogeneous graph&#xff09;上学习节点表示。通过Graph Transformer层&#xff0c;模型能将异构图转换为由meta-path定义的多个新图&#xff0c;这些meta-paths具有任意的边类型和长度&am…

运行Spring Boot项目时[ java: 错误: 不支持发行版本 17 ]

项目场景&#xff1a; 使用IDEA的Spring Initializr构建的Spring boot项目在构建完成后运行出错 问题描述&#xff1a; 用Spring Initializr创建了Spring Boot 项目后&#xff0c;运行时报错&#xff1a; “错误:java: 错误: 不支持发行版本 17”根据错误信息得知&#xff…

Vue学习笔记(5. 计算属性,监视器(侦听器))

1. 计算属性&#xff08;computed&#xff09; (1) get方式 初期显示 改变值后&#xff08;hello -> hello1&#xff09;计算属性allStr跟随变更 (2) get set方式 页面初期显示 改变值&#xff08;hello -> hello1&#xff09;计算属性的get会监控到变更&#xff0c;使…

国产SSD、内存卷哭国外大厂,三星宣布减产涨价在路上了

PC 圈有一句话是这么说的&#xff1a;论价格屠夫还得看国产品牌&#xff01; 可不是嘛&#xff0c;国产长鑫、长江算是彻底将全球存储芯片市场搅局者这一「骂名」坐实了&#xff01; 不说特别早期&#xff0c;前几年吧&#xff0c;普通单条 8G DDR4 内存都能卖到六七百元&…

C++ 红黑树

1.红黑树的概念 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存储位表示结点的颜色&#xff0c;可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制&#xff0c;红黑树确保没有一条路 径会比其他路径长出俩倍&#xff0c;因…

D. Many Perfect Squares

题目链接&#xff1a;Problem - D - Codeforces 题意&#xff1a;给你一个数组&#xff0c;大小不超过50个。 问你让他们全部加上一个x&#xff0c;构造出来最多能够有多少个完全平方数。 思路&#xff1a; 先对数组排个序&#xff0c;首先它最少一定是有一个的&#xff0c…