函数对象
函数对象基本使用
重载 函数调用操作符 的类,其对象被称为函数对象;函数对象使用重载的()时,行为类似函数调用,也叫仿函数
函数对象(仿函数)本质是一个类,不是一个函数
特点:使用时可以像普通函数那样调用,可以有参数,返回值
函数对象超出普通函数的概念,可以有自己的状态
函数对象可以作为参数传递
#include<iostream>
#include<string>
using namespace std;
class MYADD {
public:
int operator()(int v1,int v2) { //重载 函数调用操作符() 的类,其对象被称为函数对象;
return v1 + v2;
}
};
class MYprint {
public:
MYprint() {
this->count = 0;
}
void operator()(string sts) {
cout << sts << endl;
this->count++;
}
int count;//内部自己的状态
};
void doprint(MYprint& mm, string tt1) {
mm(tt1);
}
void test01() {
MYADD my;
cout << my(2, 4) << endl;
MYprint mp;
mp("uihihii");
mp("uihihii");
mp("uihihii");
mp("uihihii");
mp("uihihii");
cout << "myptint调用次数:\t" << mp.count << endl;
//函数对象可以作为参数传递
MYprint mm;
doprint(mm, "erfweta");
doprint(mp, "erfweta");
}
int main() {
test01();
system("pause");
return 0;
}
谓词——一元谓词
返回BOOL类型的仿函数称为谓词
operator()接受一个参数,一元谓词;两个参数,二元谓词
//find_if,定义,找有没有大于五的数字
_EXPORT_STD template <class _InIt, class _Pr>
_NODISCARD _CONSTEXPR20 _InIt find_if(_InIt _First, const _InIt _Last, _Pr _Pred) { // find first satisfying _Pred
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
for (; _UFirst != _ULast; ++_UFirst) {
if (_Pred(*_UFirst)) { //如果仿函数取出的值为真,结束循环,假则继续循环
break;
}
}
_Seek_wrapped(_First, _UFirst);
return _First; //返回一个迭代器
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class great5 {
public:
bool operator()(int val) {
return val > 5;
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//great5()匿名的函数对象,省掉了创建对象的步骤
vector<int>::iterator it= find_if(v.begin(), v.end(), great5());
if (it == v.end()) {
cout << "mei you find" << endl;
}
else {
cout << "find\t" << *it<<endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
谓词——二元谓词
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class great5 {
public:
bool operator()(int val1,int val2) {
return val1>val2;
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
int t = rand()%300;
v.push_back(t);
}
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(),great5());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
内建函数对象
STL内建的函数对象,头文件#include<fuctional>
算术仿函数
四则运算,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>//取yu
template <class T> T negate<T>//取反 一元
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
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>//取yu
template <class T> T negate<T>//取反——一元
*/
void test01() {
negate<int>n;
cout<<n(50)<<endl;
plus<int>p;
cout << p(34, 23) << endl;
minus<int>m;
cout << m(34, 23) << endl;
multiplies<int>ml;
cout << ml(34, 23) << endl;
modulus<int>mo;
cout << mo(34, 23) << endl;
cout << mo(200, 23) << endl;
divides<int>de;
cout << de(34, 23) << endl;
}
int main() {
test01();
system("pause");
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 great_equal<T>//>=
template <class T> bool less <T>//<
template <class T> bool less_equal <T>//<=
//SORT
//有参
_EXPORT_STD template <class _RanIt, class _Pr>
_CONSTEXPR20 void sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred) { // order [_First, _Last)
_Adl_verify_range(_First, _Last);
const auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
_Sort_unchecked(_UFirst, _ULast, _ULast - _UFirst, _Pass_fn(_Pred));
}
//默认,less<>{}
_EXPORT_STD template <class _RanIt>
_CONSTEXPR20 void sort(const _RanIt _First, const _RanIt _Last) { // order [_First, _Last)
_STD sort(_First, _Last, less<>{});
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
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 great_equal<T>//>=
template <class T> bool less <T>//<
template <class T> bool less_equal <T>//<=
*/
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
int t = rand() % 300;
v.push_back(t);
}
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(),greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
逻辑仿函数
基本用不到
template <class T> bool logical_and<T>//&&
template <class T> bool logical_or<T>//||
template <class T> bool logical_not<T>//!
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
template <class T> bool logical_and<T>//&&
template <class T> bool logical_or<T>//||
template <class T> bool logical_not<T>//!
*/
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
//int p = rand() % 300-200;
int p = rand() % 300;
v.push_back(p%2);
}
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
vector<int>v2;
v2.resize(v.size());//先开辟好大小
transform(v.begin(), v.end(), v2.begin(), logical_not<int>());
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用遍历算法
算法头文件主要:<algorithm>,<function>,<numeric>
<algorithm>STL头文件中最大的一个,包括比较,交换,查找,遍历操作,修改等
<function>定义了模板类,用以声明函数对象
<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
FOR_EACH
for_each//遍历; for_each(iterator beg,iterator end,_func)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
for_each//遍历 ;for_each(iterator beg,iterator end,_func)
transform//遍历+搬运
*/
void print1(int val) {
cout << val << " ";
}
class print02 {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
int p = rand() % 300;
v.push_back(p);
}
for_each(v.begin(), v.end(), print1);//普通函数把函数名放上
cout << "_______" << endl;
for_each(v.begin(), v.end(), print02());//函数对象要加对象
cout << "_______" << endl;
}
int main() {
test01();
system("pause");
return 0;
}
TRANSFORM
transform//遍历+搬运 ;transform ( iterator beg1 , iterator end1 , iterator beg2 , _func)(源BEG,源END,目标BEG,函数名)
提前开辟空间
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
/*
for_each//遍历 ;for_each(iterator beg,iterator end,_func)
transform//遍历+搬运 ;transform(iterator beg1,iterator end1,iterator beg2,_func)(源BEG,源END,目标BEG,函数名)
*/
void print1(int val) {
cout << val << " ";
}
class print02 {
public:
int operator()(int val) {
return val*100;
}
};
void test01() {
vector<int>v;
vector<int>v2;
for (int i = 0; i < 10; i++) {
int p = rand() % 300;
v.push_back(p);
}
v2.resize(v.size());
for_each(v2.begin(), v2.end(), print1);
cout << "_______" << endl;
transform(v.begin(), v.end(), v2.begin(),print02());
for_each(v2.begin(), v2.end(), print1);
cout << "_______" << endl;
}
int main() {
test01();
system("pause");
return 0;
}
常用查找算法
find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分法查找
count //统计元素个数
count_if //按条件统计元素个数
FIND
find //查找元素,返回POS或者END() FIND ( BEG ,END ,VALUE )
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Person {
public:
Person(string n, int a) {
this->_name = n;
this->_age = a;
}
bool operator==(const Person&p) {
if (this->_name == p._name && this->_age == p._age) {
return true;
}
else {
return false;
}
}
string _name;
int _age;
};
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 << " no find " << endl;
}
else {
cout << " find :" << *it << endl;
}
}
void test02() {
vector<Person>p;
Person p0("fsdef", 23);
Person p1("复合工艺", 28);
Person p2("粉色", 9);
Person p3("得分·", 45);
Person p4("啊上网服务", 53);
p.push_back(p0);
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
vector<Person>::iterator it = find(p.begin(), p.end(), p2);
if (it == p.end()) {//需要重载==
cout << " no find " << endl;
}
else {
cout << " find :" << it->_name << it->_age << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}