概念:
1、重载函数调用操作符()的类,其对象常称为函数对象
2、函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:函数对象(仿函数)是一个类,不是一个函数
特点:
1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
2、函数对象超出普通函数的概念,函数对象可以有自己的状态
3、函数对象可以作为参数传递
#include
using namespace std;
#include
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
using namespace std;
#include
#include
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
实现四则运算
其中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
using namespace std;
#include
// 一元运算,取反
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
using namespace std;
#include
#include
#include
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> // 逻辑非仿函数
由头文件
组成
是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等
定义了一些模板类,用以声明函数对象
体积很小,只包含几个在序列上面进行简单数学运算的模板函数
遍历容器
函数原型:
#include
for_each(iterator beg, iterator end, _func);
_func:函数或函数对象,类似于回调函数,例如
#include
using namespace std;
#include
#include
// 普通函数
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;
}
搬运容器到另一个容器中
函数原型:
#include
transform(iterator beg1, iterator end1, iterator beg2, _func);
beg1: 源容器开始迭代器
end1: 源容器结束迭代器
beg2: 目标容器开始迭代器
_func:函数或函数对象
例如:
#include
using namespace std;
#include
#include
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;
}
查找元素
查到返回元素的迭代器,否则返回end()
函数原型:
#include
find(iterator beg, iterator end, value);
beg: 容器开始迭代器
end: 容器结束迭代器
value:查找的元素
可以使用内置数据类型,也可以使用自定义数据类型(例如class),但自定义数据类型需要实现运算符==的重载,例如:
#include
using namespace std;
#include
#include
#include
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;
}
按条件查找
查到返回元素的迭代器,否则返回end()
函数原型:
#include
find(iterator beg, iterator end, _Pred);
beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:函数或者是谓词(返回bool类型的仿函数)
#include
using namespace std;
#include
#include
#include
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;
}
查找相邻重复元素
返回相邻重复元素的第一个位置的迭代器
函数原型:
#include
adjacent_find(iterator beg, iterator end);
beg: 容器开始迭代器
end: 容器结束迭代器
二分查找法
查找指定元素是否存在,查到返回true,否则返回false
注意:无序序列中不可用
函数原型:
#include
bool binary_search(iterator beg, iterator end, value);
beg: 容器开始迭代器
end: 容器结束迭代器
value:查找的元素
统计元素个数
函数原型:
#include
cout(iterator beg, iterator end, value);
beg: 容器开始迭代器
end: 容器结束迭代器
value:统计的元素
内置数据类型和自定义数据类型,自定义数据类型需要运算符==重载
按条件统计元素个数
函数原型:
#include
cout_if(iterator beg, iterator end, _Pred);
beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:谓词,bool类型
内置数据类型和自定义数据类型
对容器内元素进行排序
按值查找元素,找到返回指定位置迭代器,否则返回end()
函数原型:
#include
sort(iterator beg, iterator end, _Pred);
beg: 容器开始迭代器
end: 容器结束迭代器
_Pred:谓词或內建函数
洗牌,指定范围内的元素随机调整次序
函数原型:
#include
random_shuffle(iterator beg, iterator end);
beg: 容器开始迭代器
end: 容器结束迭代器
需要加随机数种子,以此来保证每次打乱顺序都不一样
srand((unsigned int)time(NULL));
容器元素合并,并存储到另一容器中
注意:两个容器必须是有序的
函数原型:
#include
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
beg1: 容器1开始迭代器
end1: 容器1结束迭代器
beg2: 容器2开始迭代器
end2: 容器2结束迭代器
dest:目标容器开始迭代器
注意目标容器需要预设空间
反转指定范围的元素
函数原型:
#include
reverse(iterator beg, iterator end);
beg: 开始迭代器
end: 结束迭代器
容器内指定范围内元素拷贝到另一容器中
函数原型:
#include
copy(iterator beg, iterator end, iterator dest);
beg: 开始迭代器
end: 结束迭代器
dest:目标容器起始迭代器
注意目标容器需要预设空间
将容器内指定范围的旧元素修改为新元素
函数原型:
#include
replace(iterator beg, iterator end, oldvalue, newvalue);
beg: 开始迭代器
end: 结束迭代器
oldvalue:旧元素
newvalue:新元素
容器内指定范围满足条件的元素替换为新元素
函数原型:
#include
replace_if(iterator beg, iterator end, _Pred, newvalue);
beg: 开始迭代器
end: 结束迭代器
_Pred:谓词
newvalue:新元素
互换两个容器的元素
函数原型:
#include
swap(container c1, container c2);
c1: 容器1
c2: 容器2
注意:同等类型容器
计算区间内容器元素累计总和
函数原型:
#include
accumulate(iterator beg, iterator end, value);
beg:开始迭代器
end:结束迭代器
value:起始累加值
向容器中指定区间内填充为指定的元素
函数原型:
#include
fill(iterator beg, iterator end, value);
beg:开始迭代器
end:结束迭代器
value:填充的值
求两个容器的交集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即交集中最后一个元素的位置
函数原型:
#include
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器
求两个容器的并集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即并集中最后一个元素的位置
函数原型:
#include
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器
求两个容器的差集
注意:两个容器必须为有序序列
返回值:目标容器的最后一个元素的迭代器地址,即差集中最后一个元素的位置
函数原型:
#include
ser_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
beg1:容器1开始迭代器
end1:容器1结束迭代器
beg2:容器2开始迭代器
end2:容器2结束迭代器
dest:目标容器开始迭代器