您现在的位置是:主页 > news > wordpress站内计费搜索/seo网络运营
wordpress站内计费搜索/seo网络运营
admin2025/4/30 16:23:19【news】
简介wordpress站内计费搜索,seo网络运营,泰安网站建设焦点网络,微信的公众平台网站开发STL-函数对象(仿函数)、谓词、内建函数对象 文章目录STL-函数对象(仿函数)、谓词、内建函数对象前言1. 函数对象1.1 函数对象概念1.2 函数对象使用2. 谓词2.1 谓词概念2.2 一元谓词2.3 二元谓词3.内建函数对象3.1 内建函数对象意义…
STL-函数对象(仿函数)、谓词、内建函数对象
文章目录
- STL-函数对象(仿函数)、谓词、内建函数对象
- 前言
- 1. 函数对象
- 1.1 函数对象概念
- 1.2 函数对象使用
- 2. 谓词
- 2.1 谓词概念
- 2.2 一元谓词
- 2.3 二元谓词
- 3.内建函数对象
- 3.1 内建函数对象意义
- 3.2 算术仿函数
- 3.2.1 plus():加法仿函数
- 3.2.2 minus():减法仿函数
- 3.2.3 multiplies():乘法仿函数
- 3.2.4 divides():除法仿函数
- 3.2.5 modulus():取模仿函数
- 3.2.6 negate():取反仿函数
- 3.3 关系仿函数
- 3.3.1 equal_to():等于仿函数
- 3.3.2 not_equal_to():不等于仿函数
- 3.3.3 greater():大于仿函数
- 3.3.4 greater_equal():大于等于仿函数
- 3.3.5 less():小于仿函数
- 3.3.6 less_equal():小于等于仿函数
- 3.4 逻辑仿函数
- 3.4.1 logical_and():逻辑与仿函数
- 3.4.2 logical_or():逻辑或仿函数
- 3.4.3 logical_not():逻辑非仿函数
- 总结
前言
本文包含函数对象概念、函数对象使用、谓词概念、 一元谓词、 二元谓词、内建函数对象意义、算术仿函数(plus()、minus()、multiplies()、divides()、modulus()、negate())、关系仿函数(equal_to()、not_equal_to()、greater()、greater_equal()、less()、less_equal())、逻辑仿函数(logical_and()、logical_or()、logical_not())。
1. 函数对象
1.1 函数对象概念
概念:
(1)、重载函数调用操作符的类,其对象常称为函数对象
(2)、函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:
(1)、函数对象(仿函数)是一个类,不是一个函数
1.2 函数对象使用
特点:
(1)、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
// 函数对象(仿函数)#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间// 1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
class fun_Add {public:int operator()(int a, int b) { // 重载函数调用操作符(),返回两数之和return a + b;}
};void test() {fun_Add fun_add; // 通过fun_Add类,创建一个fun_add函数对象cout << "两数之和为:" << fun_add(10, 20) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
(2)、函数对象超出普通函数的概念,函数对象可以有自己的状态
// 函数对象(仿函数)#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间// 2、函数对象可以有自己的状态;函数对象超出普通函数的概念
class fun_Print {public:fun_Print() { // 无参构造函数,初始化count为0this->count = 0;}void operator()(string str) { // 重载函数调用操作符(),打印传入的参数cout << str << endl;count++; // 统计使用次数,每调用一次加1}int count; // 记录内部自己的状态;如果是普通函数,需要声明全局变量或静态变量,来记录调用次数(普通函数不是类,没有成员属性)
};void test() {fun_Print fun_print; // 通过fun_Print类,创建一个fun_print函数对象fun_print("Learning C++");fun_print("学习使用我快乐!");fun_print("123");cout << "fun_print调用次数为:" << fun_print.count << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
(3)、函数对象可以作为参数传递
// 函数对象(仿函数)#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间class fun_Print {public:void operator()(string str) { // 重载函数调用操作符(),打印传入的参数cout << str << endl;}
};// 3、函数对象可以作为参数传递
void do_Print(fun_Print& fp, string str) { // 函数体,打印字符串fp(str); // fun_Print函数fun_print对象作为一个参数,向一个do_Print函数中传递,传递&mp,传递之后,利用自身重载的函数调用操作符(),调用函数
}void test() {fun_Print fun_print; // 通过fun_Print类,创建一个print函数对象do_Print(fun_print, "Hello C++"); // 利用函数对象fun_print,以及传入打印的参数"Hello C++",间接调用仿函数fun_Print
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
2. 谓词
2.1 谓词概念
概念:
(1)、返回bool类型的仿函数称为谓词
(2)、如果operator()接受一个参数,那么叫做一元谓词
(3)、如果operator()接受两个参数,那么叫做二元谓词
2.2 一元谓词
// 一元谓词#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <vector>
#include <algorithm>// 结构体中定义一元谓词,用法和类的用法很想,struct中定义的函数和变量默认为public,但class中的则是默认为private
struct Greater_5 {bool operator()(int i) { // 重载函数调用符()的类,且返回bool类型,接收一个参数的仿函数,为一元谓词return i > 5;}
};void test() {vector<int> v; // 创建单端数组容器v,使用时必须包含头文件vectorfor (int i = 0; i < 10; i++) {v.push_back(i); // 向单端数组容器中插入十个数}// 查找容器中,有没有大于5的数字// Greater_5()匿名函数对象;正常创建一个对象,需通过类Greater_5,创建一个对象,调用对象属性或方法;不想创建对象或给对象起名,可以使用匿名函数对象// find_if():按条件查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置;使用时需包含头文件algorithmvector<int>::iterator it = find_if(v.begin(), v.end(), Greater_5()); // 返回值类型为迭代器if (it == v.end()) { // 判断返回值是否是v的end()结束迭代器,如果是,则没找到;如果不是,则找到了,返回指定位置迭代器cout << "没找到!" << endl;}else {cout << "找到: " << *it << endl; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
2.3 二元谓词
// 二元谓词#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <vector>
#include <algorithm>class Fun_Compare {public:bool operator()(int a, int b) { // 重载函数调用符()的类,且返回bool类型,接收两个参数的仿函数,为二元谓词return a > b; // 降序}
};void test() {vector<int> v; // 创建单端数组容器v,使用时必须包含头文件vectorv.push_back(20); // 向单端数组容器中插入数据v.push_back(60);v.push_back(80);v.push_back(30);v.push_back(40);// sort():将区间内的元素进行排序,默认从小到大排序;使用时需包含头文件algorithmsort(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { // vector<int>::iterator 拿到vector<int>这种容器的迭代器类型;每个容器都有一个专属的迭代器类型cout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl; // 换行cout << "---------------------------------" << endl;// sort():区间内的元素按照用户指定的顺序Fun_Compare()排列sort(v.begin(), v.end(), Fun_Compare()); // Fun_Compare()匿名函数对象;正常创建一个对象,需通过类Fun_Compare,创建一个对象,调用对象属性或方法;不想创建对象或给对象起名,可以使用匿名函数对象for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl; // 换行
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.内建函数对象
3.1 内建函数对象意义
概念:
STL内建了一些函数对象
分类:
(1)、算术仿函数
(2)、关系仿函数
(3)、逻辑仿函数
用法:
(1)、这些仿函数所产生的对象,用法和一般函数完全相同;
(2)、使用内建函数对象,需要引入头文件 #include <functional>
3.2 算术仿函数
功能描述:
(1)、实现四则运算
(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>
取反仿函数
3.2.1 plus():加法仿函数
// 算术仿函数:加法仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// plus;二元仿函数,加法plus<int> p; // <int>模板参数可以写一个,默认认为传入的都是同种类型,不存在int和float等不同类型进行相加cout << p(10, 20) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.2.2 minus():减法仿函数
// 算术仿函数:减法仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// minus;二元仿函数,减法minus<int> m; // <int>模板参数可以写一个,默认认为传入的都是同种类型,不存在int和float等不同类型进行相减cout << m(20, 10) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.2.3 multiplies():乘法仿函数
// 算术仿函数:乘法仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// multiplies;二元仿函数,乘法multiplies<int> m; // <int>模板参数可以写一个,默认认为传入的都是同种类型cout << m(10, 20) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.2.4 divides():除法仿函数
// 算术仿函数:除法仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// divides;二元仿函数,除法divides<int> d; // <int>模板参数可以写一个,默认认为传入的都是同种类型cout << d(10, 20) << endl;divides<int> d1;cout << d1(20, 10) << endl;divides<int> d2;cout << d2(10, 2.5) << endl;divides<double> d3;cout << d3(44.88, 22.5555) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.2.5 modulus():取模仿函数
// 算术仿函数:取模仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// modulus;二元仿函数,取模modulus<int> m; // <int>模板参数可以写一个,默认认为传入的都是同种类型cout << m(10, 20) << endl;modulus<int> m1;cout << m1(20, 10) << endl;modulus<int> m2;cout << m2(-10, 3) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.2.6 negate():取反仿函数
// 算术仿函数:取反仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件void test() {// negate;二元仿函数,取反negate<int> n; // <int>模板参数可以写一个,默认认为传入的都是同种类型cout << n(10) << endl;negate<int> n1;cout << n1(-5) << endl;negate<double> n2;cout << n2(-10.88) << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
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>
小于等于
3.3.1 equal_to():等于仿函数
// 关系仿函数:等于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<int> v1 = { 10,20,30,40,50,60 };vector<int> v2 = { 10,20,30,100,50,60 };// 成对的声明指针pair<vector<int>::iterator, vector<int>::iterator>pairs1;// 使用mismatch()函数搜索v1和v2之间的第一个不匹配;使用mismatch()函数需包含algorithm头文件pairs1 = mismatch(v1.begin(), v1.end(), v2.begin(), equal_to<int>());// 打印不匹配的配对cout << "第一个容器的第一个不匹配元素为: " << *pairs1.first << endl;cout << "第二个容器的第一个不匹配元素为: " << *pairs1.second << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.3.2 not_equal_to():不等于仿函数
// 关系仿函数:不等于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<int> v1 = { 10,20,30,40,50,60 };vector<int> v2 = { 10,20,30,100,50,60 };// 成对的声明指针pair<vector<int>::iterator, vector<int>::iterator>pairs1;// 使用mismatch()函数搜索v1和v2之间的第一个匹配项;使用mismatch()函数需包含algorithm头文件pairs1 = mismatch(v1.begin(), v1.end(), v2.begin(), not_equal_to<int>());// 打印配对cout << "第一个容器的第一个匹配元素为: " << *pairs1.first << endl;cout << "第二个容器的第一个匹配元素为: " << *pairs1.second << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.3.3 greater():大于仿函数
// 关系仿函数:大于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<int> v = { 90,20,10,70,80,40 };cout << "排序前:" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it ++ ) { // 遍历单端数组cout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl;sort(v.begin(), v.end(), greater<int>()); // // STL内建仿函数:大于仿函数;内建函数对象,可修改模板类型<int>cout << "排序后:" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.3.4 greater_equal():大于等于仿函数
// 关系仿函数:大于等于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>int main() {// 初始化vectorvector<int> v = { 90,-20,10,-70,80,40,0 };// 统计单端数组容器v中,大于等于0的元素个数// count_if():按条件统计元素出现次数// bind2nd():用于将一个二元函数转换成一元函数int cx = count_if(v.begin(), v.end(), bind2nd(greater_equal<int>(), 0));cout << "There are " << cx << " non-negative elements.\n"; // \n换行system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.3.5 less():小于仿函数
// 关系仿函数:小于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<int> v = { 90,20,10,70,80,40 };cout << "排序前:" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it ++ ) { // 遍历单端数组cout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl;sort(v.begin(), v.end(), less<int>()); // // STL内建仿函数:小于仿函数;内建函数对象,可修改模板类型<int>cout << "排序后:" << endl;for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.3.6 less_equal():小于等于仿函数
// 关系仿函数:小于等于仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>int main() {// 初始化vectorvector<int> v = { 90,-20,10,-70,80,40,1 };// 统计单端数组容器v中,小于等于0的元素个数// count_if():按条件统计元素出现次数// bind2nd():用于将一个二元函数转换成一元函数int cx = count_if(v.begin(), v.end(), bind2nd(less_equal<int>(), 0));cout << "There are " << cx << " non-negative elements.\n"; // \n换行system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.4 逻辑仿函数
功能描述:
实现逻辑运算
函数原型:
template<class T> bool logical_and<T>
逻辑与仿函数
template<class T> bool logical_or<T>
逻辑或仿函数
template<class T> bool logical_not<T>
逻辑非仿函数
3.4.1 logical_and():逻辑与仿函数
// 逻辑仿函数:逻辑与仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<bool> v = { true,true,false,false,true,false };cout << "搬运前:" << endl;for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) { // 遍历单端数组vcout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl;// 将单端容器v搬运到单端容器v_Target中,并执行逻辑与运算vector<bool> v_Target;// resize()重置大小,扩大到和单端容器v一样的大小;目标容器必须开辟好大小,否则就是收缩不能搬运,会报错v_Target.resize(v.size());// transform()搬运算法;第一个参数:源容器的begin()迭代器;第二个参数:源容器的end()迭代器;第三个参数:目标容器的起始迭代器;第四个参数:仿函数transform(v.begin(), v.end(), v_Target.begin(), bind2nd(logical_and<bool>(), true)); // bind2nd(logical_and<bool>(), true):与true进行运算cout << "搬运后:" << endl;for (vector<bool>::iterator it = v_Target.begin(); it != v_Target.end(); it++) { cout << *it << " ";}cout << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.4.2 logical_or():逻辑或仿函数
// 逻辑仿函数:逻辑或仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<bool> v = { true,true,false,false,true,false };cout << "搬运前:" << endl;for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) { // 遍历单端数组vcout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl;// 将单端容器v搬运到单端容器v_Target中,并执行逻辑或运算vector<bool> v_Target;// resize()重置大小,扩大到和单端容器v一样的大小;目标容器必须开辟好大小,否则就是收缩不能搬运,会报错v_Target.resize(v.size());// transform()搬运算法;第一个参数:源容器的begin()迭代器;第二个参数:源容器的end()迭代器;第三个参数:目标容器的起始迭代器;第四个参数:仿函数transform(v.begin(), v.end(), v_Target.begin(), bind2nd(logical_or<bool>(), true)); // bind2nd(logical_and<bool>(), true):与true进行运算cout << "搬运后:" << endl;for (vector<bool>::iterator it = v_Target.begin(); it != v_Target.end(); it++) { cout << *it << " ";}cout << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
3.4.3 logical_not():逻辑非仿函数
// 逻辑仿函数:逻辑非仿函数#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间#include <functional> // 内建函数对象头文件
#include <vector>
#include <algorithm>void test() {// 初始化vectorvector<bool> v = { true,true,false,false,true,false };cout << "搬运前:" << endl;for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) { // 遍历单端数组vcout << *it << " "; // it返回的是迭代器,迭代器本质是一个指针,使用*解引用}cout << endl;// 将单端容器v搬运到单端容器v_Target中,并执行逻辑非运算vector<bool> v_Target;// resize()重置大小,扩大到和单端容器v一样的大小;目标容器必须开辟好大小,否则就是收缩不能搬运,会报错v_Target.resize(v.size());// transform()搬运算法;第一个参数:源容器的begin()迭代器;第二个参数:源容器的end()迭代器;第三个参数:目标容器的起始迭代器;第四个参数:仿函数transform(v.begin(), v.end(), v_Target.begin(), logical_not<bool>());cout << "搬运后:" << endl;for (vector<bool>::iterator it = v_Target.begin(); it != v_Target.end(); it++) { cout << *it << " ";}cout << endl;
}int main() {test();system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果return 0; // 程序正常退出
}
总结
(1)、仿函数写法非常灵活,可以作为参数进行传递;
(2)、参数只有一个的谓词,称为一元谓词;
(3)、参数只有两个的谓词,称为二元谓词;
(4)、使用内建函数对象时,需要引入头文件 #include <functional>
;
(5)、关系仿函数中最常用的就是greater<>大于;
(6)、逻辑仿函数实际应用较少,了解即可。