1.函数对象
- 重载函数调用操作符的类,其对象称为函数对象
- 函数对象使用重载的()时,行为类似函数调用,也称为仿函数
- 函数对象(仿函数)是一个类,不是一个函数
使用时的特点:
- 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
// 1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test01()
{
MyAdd myadd;
cout << myadd(10, 20) << endl;
}
// 2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string text)
{
cout << text << endl;
this->count++;
}
int count; // 记录调用次数
};
void test02()
{
MyPrint mp;
mp("Hello");
cout << mp.count << endl;
}
// 3.函数对象可以作为参数传递
void doPrint(MyPrint &mp, string text)
{
mp(text);
}
void test03()
{
MyPrint mp;
doPrint(mp, "hello");
}
2.谓词
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,叫做一元谓词
- 如果operator()接受两个参数,叫做二元谓词
// 一元谓词
class GreaterFive
{
public:
bool operator()(int num)
{
return num > 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;
}
}
// 二元谓词
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(60);
v.push_back(50);
sort(v.begin(), v.end());
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 使用仿函数,可以改变排序规则为降序
sort(v.begin(), v.end(), MyCompare());
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
3.内建函数
- STL内建了一些函数对象(仿函数)
分类:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
用法:
- 这些仿函数所产生的对象,用法和一般函数完全相同
- 使用内建函数对象,需要引入头文件 #include<functional>
4.算术仿函数
- 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<functional>
// negate 一元仿函数 取反仿函数
void test01()
{
negate<int> n;
cout << n(50) << endl; // -50
}
// plus 二元仿函数 加法
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
5.关系仿函数
- 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> 小于等于
#include<functional>
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(50);
for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 降序
// 等价于sort(v.begin(), v.end(), MyCompare());
// greater<int>()是内建函数对象(仿函数)
sort(v.begin(), v.end(), greater<int>());
for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
6.逻辑仿函数
- template<class T> bool logical_and<T>; 逻辑与
- template<class T> bool logical_or<T>; 逻辑或
- template<class T> bool logical_not<T>; 逻辑非
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; // 1 0 1 0
// 利用逻辑非 将容器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; // 0 1 0 1
}
7.STL常用算法
- 算法主要是由头文件<algorithm><functional><numeric>组成
- <algorithm>是所有STL文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等等
- <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模版函数
- <functional>定义了一些模版类,用以声明函数对象
8.常用遍历算法
-
for_each:遍历容器
// 普通函数 void print01(int val) { cout << val << endl; } // 仿函数 class print02 { public: void operator()(int val) { cout << val << endl; } }; 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; }
-
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> v2; // 目标容器 v2.resize(v.size()); // 目标容器需要提前开辟空间 transform(v.begin(), v.end(), v2.begin(), Transform()); for_each(v2.begin(), v2.end(), Myprint()); cout << endl; }
9.常用查找算法
-
find:查找元素**(返回值是一个迭代器)**
// 查内置数据类型 void test01() { vector<int> v; for(int i=0; i<10; i++) { v.push_back(i); } 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(p.m_name == this->m_name || p.m_age == this->m_age) { return true; } return false; } string m_name; int m_age; }; // 查自定义数据类型 void test02() { vector<Person> v; Person p1("Alice", 18); Person p2("Bob", 16); Person p3("Cycal", 20); Person p4("Davil", 19); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); Person pp("Bob", 16); 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; } }
-
find_if:按条件查找元素
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); } vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive()); 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(p.m_name == this->m_name || p.m_age == this->m_age) { return true; } return false; } 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("Alice", 18); Person p2("Bob", 16); Person p3("Cycal", 22); Person p4("Davil", 19); 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; } }
-
adjacent_find:查找相邻重复元素
void test01() { vector<int> v; v.push_back(0); v.push_back(2); v.push_back(1); v.push_back(1); v.push_back(3); v.push_back(0); v.push_back(2); v.push_back(1); vector<int>::iterator it = adjacent_find(v.begin(), v.end()); if(it == v.end()) { cout << "找不到相邻重复元素" << endl; } else { cout << "找到相邻重复元素" << *it << endl; } }
-
binary_search:二分查找法(查找指定元素,查到返回true,查不到返回false)(在无序序列中不能使用)
void test01() { vector<int> v; for(int i=0; i<10; i++) { v.push_back(i); } bool tag = binary_search(v.begin(), v.end(), 9); if(tag == true) { cout << "找到了" << endl; } else { cout << "找不到" << endl; } }
-
count:统计元素个数
// 统计内置数据类型 void test01() { vector<int> v; for(int i=0; i<10; i++) { v.push_back(i); } int num = count(v.begin(), v.end(), 2); cout << num << 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(p.m_age == this->m_age) { return true; } return false; } string m_name; int m_age; }; // 统计自定义数据类型 void test02() { vector<Person> v; Person p1("Alice", 18); Person p2("Bob", 16); Person p3("Cycal", 22); Person p4("Davil", 16); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); Person pp("Tim", 16); int num = count(v.begin(), v.end(), pp); cout << num << endl; }
-
count_if:按条件统计元素个数
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); } int count = count_if(v.begin(), v.end(), GreaterFive()); cout << count << endl; }
class Person { public: Person(string name, int age) { this->m_name = name; this->m_age = age; } string m_name; int m_age; }; class MyCompare { public: bool operator()(const Person &p) { return p.m_age > 18; } }; // 查自定义数据类型 void test02() { vector<Person> v; Person p1("Alice", 18); Person p2("Bob", 16); Person p3("Cycal", 20); Person p4("Davil", 19); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); int count = count_if(v.begin(), v.end(), MyCompare()); cout << count << endl; }