【基础】【四】【函数对象】【常用算法】C++学习日记

Day 12

函数对象

概念
    1、重载函数调用操作符()的类,其对象常称为函数对象
    2、函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:函数对象(仿函数)是一个类,不是一个函数
特点
    1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
    2、函数对象超出普通函数的概念,函数对象可以有自己的状态
    3、函数对象可以作为参数传递

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

class MyAdd
{
public:
	MyAdd()
	{
		this->count = 0;
	}
	int operator()(int v1, int v2)
	{
		this->count++;
		return v1 + v2;
	}
	void operator()()
	{
		this->count++;
	}
	int count;
};
// 1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test01()
{
	MyAdd myadd;
	cout << myadd(10, 20) << endl;
}
// 2、函数对象可以有自己的状态
void test02()
{
	MyAdd myadd;
	myadd();
	myadd();
	myadd();
	myadd();
	cout << myadd.count << endl;
}
// 3、函数对象可以作为参数传递
void print(MyAdd& myadd)
{
	cout << myadd.count << endl;
}
void test03()
{
	MyAdd myadd;
	print(myadd);
}

int main()
{
	test01();
	test02();
	test03();
	return 0;
}
谓词

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

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class My
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
	bool operator()(int val1, int val2)
	{
		return val1 > val2;
	}
};
// 一元谓词,查找容器中大于5的数字
void test01()
{
	vector<int> arr;
	for (int i = 0; i < 10; i++)
	{
		arr.push_back(i);
	}
	// find_if函数,找到返回迭代器位置,否则返回end()
	vector<int>::iterator it = find_if(arr.begin(), arr.end(), My());
	if (it == arr.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << *it << endl;
	}

}
// 二元谓词,降序排序
void test02()
{
	vector<int> arr;
	arr.push_back(10);
	arr.push_back(50);
	arr.push_back(40);
	arr.push_back(20);
	arr.push_back(60);
	arr.push_back(70);
	sort(arr.begin(), arr.end(), My());
	for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}
内建函数对象

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

算术仿函数

实现四则运算
其中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>      // 取反仿函数
#include<iostream>
using namespace std;
#include<functional>
// 一元运算,取反
void test01()
{
	negate<int>n;
	cout << n(10) << endl;

}
// 二元运算,相加
void test02()
{
	plus<int>n;
	cout << n(10, 20) << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}
关系仿函数

实现关系对比
仿函数原型:

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实现降序排序,例如

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include<functional>
int main()
{
	vector<int> arr;
	arr.push_back(10);
	arr.push_back(50);
	arr.push_back(30);
	arr.push_back(40);
	arr.push_back(20);
	sort(arr.begin(), arr.end(), greater<int>());
	for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

sort函数有两种重载方式,一种是接收谓词,另一种接收关系仿函数(默认是less)

逻辑仿函数

实现逻辑运算
仿函数原型:

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

常用算法

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

遍历算法

for_each

遍历容器
函数原型:

#include<algorithm>
for_each(iterator beg, iterator end, _func);

_func:函数或函数对象,类似于回调函数,例如

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
// 普通函数
void print(int val)
{
	cout << val << " ";
}
// 仿函数
class Print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
int main()
{
	vector<int> arr;
	for (int i = 0; i < 10; i++)
	{
		arr.push_back(i);
	}
	// 普通函数
	for_each(arr.begin(), arr.end(), print);
	cout << endl;
	// 仿函数
	for_each(arr.begin(), arr.end(), Print());
	return 0;
}
transform

搬运容器到另一个容器中
函数原型:

#include<algorithm>
transform(iterator beg1, iterator end1, iterator beg2, _func);

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

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
class Print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
class TransForm
{
public:
	int operator()(int val)
	{
		return val + 1;
	}
};
int main()
{
	vector<int> arr;
	for (int i = 0; i < 10; i++)
	{
		arr.push_back(i);
	}
	vector<int> t;
	// 新建的t容器容量为零
	// 需要提前开辟足够空间,多余空间用0填充
	t.resize(arr.size());
	transform(arr.begin(), arr.end(), t.begin(), TransForm());
	for_each(t.begin(), t.end(), Print());
	return 0;
}

查找算法

find

查找元素
查到返回元素的迭代器,否则返回end()
函数原型:

#include<algorithm>
find(iterator beg, iterator end, value);

beg: 容器开始迭代器
end: 容器结束迭代器
value:查找的元素
可以使用内置数据类型,也可以使用自定义数据类型(例如class),但自定义数据类型需要实现运算符==的重载,例如:

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person& p)
	{
		if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

// 内置数据类型
void test1()
{
	vector<int> arr;
	for (int i = 0; i < 10; i++)
	{
		arr.push_back(i);
	}
	vector<int>::iterator it = find(arr.begin(), arr.end(), 10);
	if (it == arr.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << *it << endl;
	}
}
// 自定义数据类型
void test2()
{
	vector<Person> arr;
	arr.push_back(Person("李四", 20));
	arr.push_back(Person("张三", 18));
	arr.push_back(Person("赵五", 22));
	arr.push_back(Person("王六", 25));
	vector<Person>::iterator it = find(arr.begin(), arr.end(), Person("张三", 18));
	if (it == arr.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "姓名:" << it->m_Name << "  年龄:" << it->m_Age << endl;
	}
}
int main()
{
	// 内置数据类型
	test1();
	// 自定义数据类型
	test2();
	return 0;
}
find_if

按条件查找
查到返回元素的迭代器,否则返回end()
函数原型:

#include<algorithm>
find(iterator beg, iterator end, _Pred);

beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:函数或者是谓词(返回bool类型的仿函数)

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
class GreaterFive
{
public:
	bool operator()(int val)
	{
		if (val > 5)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person& p)
	{
		if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};
class Greate20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};
// 内置数据类型
void test1()
{
	vector<int> arr;
	for (int i = 0; i < 10; i++)
	{
		arr.push_back(i);
	}
	vector<int>::iterator it = find_if(arr.begin(), arr.end(), GreaterFive());
	if (it == arr.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << *it << endl;
	}
}
// 自定义数据类型
void test2()
{
	vector<Person> arr;
	arr.push_back(Person("李四", 20));
	arr.push_back(Person("张三", 18));
	arr.push_back(Person("赵五", 22));
	arr.push_back(Person("王六", 25));
	vector<Person>::iterator it = find_if(arr.begin(), arr.end(), Greate20());
	if (it == arr.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "姓名:" << it->m_Name << "  年龄:" << it->m_Age << endl;
	}
}
int main()
{
	// 内置数据类型
	test1();
	// 自定义数据类型
	test2();
	return 0;
}
adjacent_find

查找相邻重复元素
返回相邻重复元素的第一个位置的迭代器
函数原型:

#include<algorithm>
adjacent_find(iterator beg, iterator end);

beg: 容器开始迭代器
end: 容器结束迭代器

binary_search

二分查找法
查找指定元素是否存在,查到返回true,否则返回false
注意:无序序列中不可用
函数原型:

#include<algorithm>
bool binary_search(iterator beg, iterator end, value);

beg: 容器开始迭代器
end: 容器结束迭代器
value:查找的元素

cout

统计元素个数
函数原型:

#include<algorithm>
cout(iterator beg, iterator end, value);

beg: 容器开始迭代器
end: 容器结束迭代器
value:统计的元素
内置数据类型和自定义数据类型,自定义数据类型需要运算符==重载

count_if

按条件统计元素个数
函数原型:

#include<algorithm>
cout_if(iterator beg, iterator end, _Pred);

beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:谓词,bool类型
内置数据类型和自定义数据类型

排序算法

sort

对容器内元素进行排序
按值查找元素,找到返回指定位置迭代器,否则返回end()
函数原型:

#include<algorithm>
sort(iterator beg, iterator end, _Pred);

beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:谓词或內建函数

random_shuffle

洗牌,指定范围内的元素随机调整次序
函数原型:

#include<algorithm>
random_shuffle(iterator beg, iterator end);

beg: 容器开始迭代器
end: 容器结束迭代器
需要加随机数种子,以此来保证每次打乱顺序都不一样

srand((unsigned int)time(NULL));
merge

容器元素合并,并存储到另一容器中
注意:两个容器必须是有序
函数原型:

#include<algorithm>
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

beg1: 容器1开始迭代器
end1: 容器1结束迭代器
beg2: 容器2开始迭代器
end2: 容器2结束迭代器
dest:目标容器开始迭代器
注意目标容器需要预设空间

reverse

反转指定范围的元素
函数原型:

#include<algorithm>
reverse(iterator beg, iterator end);

beg: 开始迭代器
end: 结束迭代器

拷贝和替换算法

copy

容器内指定范围内元素拷贝到另一容器中
函数原型:

#include<algorithm>
copy(iterator beg, iterator end, iterator dest);

beg: 开始迭代器
end: 结束迭代器
dest:目标容器起始迭代器
注意目标容器需要预设空间

replace

将容器内指定范围的旧元素修改为新元素
函数原型:

#include<algorithm>
replace(iterator beg, iterator end, oldvalue, newvalue);

beg: 开始迭代器
end: 结束迭代器
oldvalue:旧元素
newvalue:新元素

replace_if

容器内指定范围满足条件的元素替换为新元素
函数原型:

#include<algorithm>
replace_if(iterator beg, iterator end, _Pred, newvalue);

beg: 开始迭代器
end: 结束迭代器
_Pred:谓词
newvalue:新元素

swap

互换两个容器的元素
函数原型:

#include<algorithm>
swap(container c1, container c2);

c1: 容器1
c2: 容器2
注意:同等类型容器

算数生成算法

accumulate

计算区间内容器元素累计总和
函数原型:

#include<numeric>
accumulate(iterator beg, iterator end, value);

beg:开始迭代器
end:结束迭代器
value:起始累加值

fill

向容器中指定区间内填充为指定的元素
函数原型:

#include<numeric>
fill(iterator beg, iterator end, value);

beg:开始迭代器
end:结束迭代器
value:填充的值

集合算法

set_intersection

求两个容器的交集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即交集中最后一个元素的位置
函数原型:

#include<algorithm>
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器

set_union

求两个容器的并集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即并集中最后一个元素的位置
函数原型:

#include<algorithm>
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器

ser_difference

求两个容器的差集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即差集中最后一个元素的位置
函数原型:

#include<algorithm>
ser_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值