继续教育培训网站开发,网站主关键词如何优化,章丘网站制作,先做网站先备案1、STL-函数对象
1.1函数对象
1.1.1函数对象概念
概念#xff1a;
重载函数调用操作符的类#xff0c;其对象常称为函数对象
函数对象使用重载的()时#xff0c;行为类似函数调用#xff0c;也叫仿函数
本质#xff1a;函数对象(仿函数)是一个类#xff0c;不是一个…1、STL-函数对象
1.1函数对象
1.1.1函数对象概念
概念
重载函数调用操作符的类其对象常称为函数对象
函数对象使用重载的()时行为类似函数调用也叫仿函数
本质函数对象(仿函数)是一个类不是一个函数
1.1.2 函数对象使用
特点
函数对象在使用时可以像普通函数那样调用可以有参数可以有返回值
函数对象超出普通函数的概念函数对象内部可以有自己的状态
函数对象可以作为参数传递
#include iostream
using namespace std;
#includestring
//函数对象仿函数
//函数对象在使用时可以像普通函数那样调用可以有参数可以有返回值
//函数对象超出普通函数的概念函数对象内部可以有自己的状态
//函数对象可以作为参数传递
class MyAdd
{
public:int operator()(int v1, int v2){return v1 v2;}
};
//1、函数对象在使用时可以像普通函数那样调用可以有参数可以有返回值
void test01()
{MyAdd myadd;cout myadd(10, 10) endl;
}
//2、函数对象超出普通函数的概念函数对象内部可以有自己的状态
class MyPrint
{
public:MyPrint(){this-count 0;}void operator()(string text){cout text endl;this-count;}int count;//内部自己状态
};
void test02()
{MyPrint myprint;myprint(hello world!);myprint(hello world!);myprint(hello world!);myprint(hello world!);cout MyPrint调用的次数 myprint.count endl;
}
//3、函数对象可以作为参数传递
void doPrint(MyPrint mp, string text)
{mp(text);
}
void test03()
{MyPrint myprint;doPrint(myprint, hello C);
}int main()
{test01();test02();test03();system(pause);return 0;
}
输出结果
20
hello world!
hello world!
hello world!
hello world!
MyPrint调用的次数4
hello C
请按任意键继续. . .
1.2谓词
1.2.1谓词对象
概念 返回bool类型的仿函数称为谓词 如果operator接受一个参数那么叫做一元谓词 如果operator接受二个参数那么叫做二元谓词
1.2.2一元谓词
#include iostream
using namespace std;
#includevector
#includealgorithm
//仿函数 返回值类型是bool数据类型 称为谓词
//如果operator接受一个参数那么叫做一元谓词
class GreaterFive
{
public:bool operator()(int val){return val 5;}
};void test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}//查找容器中 是否有大于5的数字//GreaterFive() 匿名函数对象vectorint::iterator it find_if(v.begin(), v.end(), GreaterFive());if (it v.end()){cout 未找到 endl;}else{cout 找到了大于5的数字为 *it endl;}
}int main()
{test01();system(pause);return 0;
}
输出结果
找到了大于5的数字为6
请按任意键继续. . .
1.2.3二元谓词
#include iostream
using namespace std;
#includevector
#includealgorithm
//仿函数 返回值类型是bool数据类型 称为谓词
//二元谓词
class MyCompare
{
public:bool operator()(int v1,int v2){return v1 v2;}
};void test01()
{vectorintv;v.push_back(30);v.push_back(20);v.push_back(50);v.push_back(10);v.push_back(40);sort(v.begin(), v.end());//默认升序for (vectorint::iterator it v.begin(); it ! v.end(); it){cout *it ;}cout endl;//使用函数对象改变算法策略变为降序sort(v.begin(), v.end(), MyCompare());cout --------------------------- endl;for (vectorint::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
请按任意键继续. . . 1.3内建函数对象
1.3.1内建函数对象意义
概念STL内建了一些函数对象
分类 算术仿函数 关系仿函数 逻辑仿函数
用法这些仿函数所产生的对象用法和一般函数完全相同。使用内建函数对象需要引入头文件#includefunctional
1.3.2算术仿函数
功能描述实现四则运算。其中negate是一元运算其他都是二元运算
仿函数原型
templateclass TT plusT //加法仿函数
templateclass TT minusT //减法仿函数
templateclass TT multipliesT //乘法仿函数
templateclass TT dividesT //除法仿函数
templateclass TT modulusT //取模仿函数
templateclass TT negateT //取反仿函数
#include iostream
using namespace std;
#includefunctional//内建函数对象头文件
//内建函数对象 算术仿函数
//templateclass TT plusT //加法仿函数
//templateclass TT minusT //减法仿函数
//templateclass TT multipliesT //乘法仿函数
//templateclass TT dividesT //除法仿函数
//templateclass TT modulusT //取模仿函数
//templateclass TT negateT //取反仿函数//negate 一元仿函数 取反仿函数
void test01()
{negateintn;cout n(50) endl;
}
//plus 二元仿函数 加法
void test02()
{plusintp;//默认传入同种数据类型不能传入不同种数据类型cout p(10, 20) endl;
}
int main()
{test01();test02();system(pause);return 0;
}
输出结果
-50
30
请按任意键继续. . .
1.3.3关系仿函数
功能描述实现关系对比
仿函数原型最常用的是大于
templateclass T bool equal_toT //等于
templateclass T bool not_equal _toT //不等于
templateclass T bool greaterT //大于
templateclass T bool greater_equalT //大于等于
templateclass T bool lessT //小于
templateclass T bool less_equalT //小于等于
#include iostream
using namespace std;
#includefunctional//内建函数对象头文件
#includevector
#includealgorithm
//内建函数对象 关系仿函数
//templateclass T bool equal_toT //等于
//templateclass T bool not_equal _toT //不等于
//templateclass T bool greaterT //大于
//templateclass T bool greater_equalT //大于等于
//templateclass T bool lessT //小于
//templateclass T bool less_equalT //小于等于//大于 greater
class MyCompare
{
public:bool operator()(int v1, int v2){return v1 v2;}
};
void test01()
{vectorintv;v.push_back(10);v.push_back(30);v.push_back(40);v.push_back(50);v.push_back(20);for (vectorint::iterator it v.begin(); it ! v.end(); it){cout *it ;}cout endl;//降序//sort(v.begin(), v.end(), MyCompare()); //自己重载仿函数sort(v.begin(), v.end(), greaterint());//内建函数对象仿函数greater效果一致for (vectorint::iterator it v.begin(); it ! v.end(); it){cout *it ;}cout endl;
}int main()
{test01();system(pause);return 0;
}
输出结果
10 30 40 50 20
50 40 30 20 10
请按任意键继续. . .
1.3.4逻辑仿函数
功能描述实现逻辑运算
函数原型
templateclass T bool logical_andT //逻辑与
templateclass T bool 1ogical_orT //逻辑或
templateclass T bool logical_notT //逻辑非
#include iostream
using namespace std;
#includefunctional//内建函数对象头文件
#includevector
#includealgorithm
//内建函数对象 关系仿函数
//templateclass T bool logical_andT //逻辑与
//templateclass T bool 1ogical_orT //逻辑或
//templateclass T bool logical_notT //逻辑非//逻辑非 logical_not
void test01()
{vectorboolv;v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(false);for (vectorbool::iterator it v.begin(); it ! v.end(); it){cout *it ;}cout endl;//利用逻辑非将容器v搬运到容器v2中并执行取反的操作vectorboolv2;v2.resize(v.size());transform(v.begin(), v.end(), v2.begin(), logical_notbool());for (vectorbool::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
请按任意键继续. . .
2、STL-常用算法
概述
算法主要是由头文件algorithmfunctionalnumeric组成
algorithm是所有STL头文件中最大的一个,范围涉及到比较,交换,查找,遍历操作,复制,修改等
numeric体积很小只包括几个在序列上面进行简单数学运算的模板函数
functional定义了一些模板类用以声明函数对象
2.1 常用遍历算法
学习目标掌握常用的遍历算法
算法简介
for_each //遍历容器
transform //搬运容器到另一个容器中
2.1.1 for_each
功能描述实现遍历容器
函数原型
for_each(iterator beg, iterator end, _func);
// 遍历算法 遍历容器元素
// beg 开始迭代器
// end 结束迭代器
// _func函数或者函数对象
#include iostream
using namespace std;
#includevector
#includealgorithm
//常用遍历算法 for_each
//普通函数
void print01(int val)
{cout val ;
}
//仿函数
class print02
{
public:void operator()(int val){cout val ;}
};
void test01()
{vectorintv;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在实际开发中是最常用遍历方法需要熟练掌握
2.1.2 transform
功能描述搬运容器到另一个容器中
函数原型
transform(iterator begl,iterator end1,iterator beg2,_func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
#include iostream
using namespace std;
#includevector
#includealgorithm
//常用遍历算法 transform
//仿函数
class Transform
{
public:int operator()(int v){return v 10;}
};class MyPrint
{
public:void operator()(int v){cout v ;}
};void test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}vectorintvTarget;//目标容器vTarget.resize(v.size());//目标容器 需要提前开辟空间否则搬运不成功transform(v.begin(), v.end(), vTarget.begin(), Transform());//可以在最后一个函数对搬运的数据进行运算或者改变 例如这次加10for_each(vTarget.begin(), vTarget.end(), MyPrint());//仿函数要放函数对象加cout endl;
}int main()
{test01();system(pause);return 0;
}
输出结果
10 11 12 13 14 15 16 17 18 19
请按任意键继续. . .
2.2常用查找算法
学习目标掌握常用的查找算法
算法简介
find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找法
count //统计元素个数
count_if //按条件统计元素个数
2.2.1 find
功能描述查找指定元素找到返回指定元素的迭代器找不到返回结束迭代器end()
函数原型
find(iterator beg, iterator end, value);
// 按值查找元素找到返回指定校置迭代器找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
#include iostream
using namespace std;
#includevector
#includestring
#includealgorithm
//常用查找算法 find
//查找 内置数据类型
void test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}vectorint::iterator pos find(v.begin(), v.end(), 5);if (pos v.end()){cout 没有找到 endl;}else{cout 找到 *pos 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;}else{return false;}}string m_Name;int m_Age;
};//查找 自定义数据类型
void test02()
{vectorPersonv;//创建数据Person p1(aaa, 10);Person p2(bbb, 20);Person p3(ccc, 30);Person p4(ddd, 40);Person pp(bbb, 20);//查找有无与此人相同的人//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找vectorPerson::iterator pos find(v.begin(), v.end(), pp);if (pos v.end()){cout 没有找到 endl;}else{cout 找到此人 姓名 pos-m_Name 年龄 pos-m_Age endl;}
}int main()
{test01();test02();system(pause);return 0;
}
输出结果
找到5
找到此人 姓名bbb 年龄20
请按任意键继续. . .
2.2.2 find_if
功能描述按条件查找元素
函数原型
find_if(iterator beg,iterator end,_Pred);
// 按值查找元素找到返回指定位置选代器找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
//_Pred 函数或者谓词(返回bool类型的仿函数)
#include iostream
using namespace std;
#includevector
#includestring
#includealgorithm
//常用查找算法 find_if
//1、查找 内置数据类型
class GreaterFive
{
public:bool operator()(int val){return val 5;}
};void test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}vectorint::iterator pos find_if(v.begin(), v.end(), GreaterFive());if (pos v.end()){cout 没有找到 endl;}else{cout 找到大于5的数字 为 *pos endl;}
}class Person
{
public:Person(string name, int age){this-m_Name name;this-m_Age age;}string m_Name;int m_Age;
};class Greater20
{
public:bool operator()(Person p){return p.m_Age 20;}
};//2、查找 自定义数据类型
void test02()
{vectorPersonv;//创建数据Person p1(aaa, 10);Person p2(bbb, 20);Person p3(ccc, 30);Person p4(ddd, 40);Person pp(bbb, 20);//查找有无与此人相同的人//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找年龄大于20的人vectorPerson::iterator pos find_if(v.begin(), v.end(), Greater20());if (pos v.end()){cout 没有找到 endl;}else{cout 找到此人 姓名 pos-m_Name 年龄 pos-m_Age endl;}
}int main()
{test01();test02();system(pause);return 0;
}
输出结果
找到大于5的数字 为6
找到此人 姓名ccc 年龄30
请按任意键继续. . .
2.2.3 adjacent_find
功能描述查找相邻重复元素
函数原型
adjacent_find(iterator beg, iterator end);
// 查找相邻重复元素,返回相邻元素的第一个位置的选代器
// beg 开始迭代器
// end 结束迭代器
#include iostream
using namespace std;
#includevector
#includealgorithm
//常用查找算法 adjacent_find
void test01()
{vectorintv;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);vectorint::iterator pos adjacent_find(v.begin(), v.end());if (pos v.end()){cout 没有找到相邻重复元素 endl;}else{cout 找到相邻重复元素 *pos endl;}
}int main()
{test01();system(pause);return 0;
}
输出结果
找到相邻重复元素3
请按任意键继续. . .
2.2.4 binary_search
功能描述查找指定元素是否存在 注意: 在无序序列中不可用
函数原型
bool binary_search(iterator beg, iterator end, value);
// 查找指定的元素查到返回true 否则false
// 注意: 在无序序列中不可用
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素n
#include iostream
using namespace std;
#includevector
#includealgorithm
//常用查找算法 binary_search
void test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}//查找容器中是否有9 元素//注意: 在无序序列中不可用//如果是无序序列结果未知//必须要有序bool ret binary_search(v.begin(), v.end(), 9);if (ret){cout 找到了元素 endl;}else{cout 未找到 endl;}
}int main()
{test01();system(pause);return 0;
}
输出结果
找到了元素
请按任意键继续. . .
2.2.5 count
功能描述统计元素个数
注意统计自定义数据类型时需要配合重载operator
函数原型
count(iterator beg, iterator end, value);
// 统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// value 统计的元素
#include iostream
using namespace std;
#includevector
#includestring
#includealgorithm
//常用查找算法 count
//1、统计内置数据类型
void test01()
{vectorintv;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);int num count(v.begin(), v.end(), 40);cout 40元素个数为 num endl;
}
//2、统计自定义数据类型
class Person
{
public:Person(string name, int age){this-m_Name name;this-m_Age age;}//需要加const防止用户修改Person//否则会报错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()
{vectorPersonv;Person p1(刘备, 35);Person p2(关羽, 35);Person p3(张飞, 35);Person p4(赵云, 30);Person p5(曹操, 40);//将人员插入到容器中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
请按任意键继续. . .
2.2.6 count_if
功能描述按条件统计元素个数
函数原型
count_if(iterator beg, iterator end, _pred);
// 按条件统计元素出现次数
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include iostream
using namespace std;
#includevector
#includestring
#includealgorithm
//常用查找算法 count_if
//1、统计内置数据类型
class Greater20
{
public:bool operator()(int val){return val 20;}
};
void test01()
{vectorintv;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(20);v.push_back(40);v.push_back(20);int num count_if(v.begin(), v.end(), Greater20());cout 大于20的元素个数为 num endl;
}
//2、统计自定义数据类型
class Person
{
public:Person(string name, int age){this-m_Name name;this-m_Age age;}//需要加const防止用户修改Person//否则会报错bool operator(const Person p){if (this-m_Age p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};class AgeGreater20
{
public:bool operator()(const Person p){return p.m_Age 20;}
};void test02()
{vectorPersonv;Person p1(刘备, 35);Person p2(关羽, 35);Person p3(张飞, 35);Person p4(赵云, 40);Person p5(曹操, 20);//将人员插入到容器中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(), AgeGreater20());cout 大于20岁的人员个数为 num endl;
}int main()
{test01();test02();system(pause);return 0;
}
输出结果
大于20的元素个数为3
大于20岁的人员个数为4
请按任意键继续. . .