学习目标:
学习内容:
1. 函数对象
1.1 函数对象概念
1.2 函数对象使用
#include<iostream>
#include<string>
using namespace std;
class MyAdd
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
//1.函数对象再调用时,可以像普通函数那样调用,可以有参数和返回值;
void test01()
{
MyAdd myadd;//创建一个函数对象
//使用myadd()来调用重载对象
cout << myadd(5, 15) << endl;
}
//2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
void operator()(string test)
{
cout << test << endl;
count++;
}
int count = 0;
};
void test02()
{
MyPrint myprint;
myprint("hello,world!!");
myprint("hello,world!!");
myprint("hello,world!!");
cout << "该函数调用了:" << myprint.count << endl;
}
//3.函数对象可以作为参数传递
void doPrint(MyPrint &myprint,string str)
{
myprint(str);
}
void test03()
{
MyPrint myprint;
doPrint(myprint, "hello,C++");
}
int main()
{
//test01();
//test02();
test03();
system("pause");
return 0;
}
2. 谓词
2.1 一元谓词
//一元谓词
#include<iostream>
#include<vector>
using namespace std;
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5 ;
}
};
void test()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//使用匿名函数GeaterFive()
//查找容器中大于5的一个数字
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
if (pos != v.end())
{
cout << "找到了" << endl;
cout << *pos << endl;
}
else
{
cout << "未找到!" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
2.2 二元谓词
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class MyCompare
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test()
{
vector<int>v;
v.push_back(30);
v.push_back(50);
v.push_back(10);
v.push_back(40);
v.push_back(20);
cout << "排序前:";
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "排序后--默认为升序:";
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "排序后--排序为降序:";
sort(v.begin(), v.end(),MyCompare());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}
总结:
一元谓词和二元谓词在使用匿名函数的时候,要注意:
- 一元谓词:必须要有迭代器接收;
- 二元谓词:不需要迭代器接收,直接使用sort()加上区间即可;
3. 内建函数
3.1 算术仿函数
//算术仿函数
//negate 一元仿函数 取反函数
//plus 二元仿函数 加法
#include<iostream>
#include<functional>
using namespace std;
void test01()
{
negate<int>n;
cout << n(50) << endl;
}
void test02()
{
plus<int>p;//默认是同种类型的相加
cout << p(10, 25) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.2 关系仿函数
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
class MyCompare
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test()
{
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());
//使用内建函数---大于
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}
3.3 逻辑仿函数
//逻辑仿函数
//逻辑仿函数在实际开发中是用不到的
//逻辑非 logical_not
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
void test()
{
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;
//利用逻辑非,将容器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;
}
int main()
{
test();
system("pause");
return 0;
}