函数对象
函数对象是内部重载了函数调用运算符“()”的类实例化出来的对象,但是使用过程中很像一个函数,所以也称为仿函数。
class Add
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
//函数对象在使用的时候和普通函数一样,拥有参数和返回值
void test01()
{
Add add;
cout << add(1, 2) << endl; //3
}
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class Print
{
public:
void operator()(string test)
{
cout << test << endl;
count++;
}
//统计函数被调用了多少次
int count = 0;
};
void test02()
{
Print print;
print("hello world");
print("hello world");
print("hello world");
print("hello world");
cout << "函数被调用了" << print.count << "次" << endl; //4次
}
//函数对象可以作为参数传递
void doPrint(Print &mp, string test)
{
mp(test);
}
void test03()
{
Print print;
doPrint(print, "hello cpp");
}
- 函数对象在使用的时候和普通函数一样,拥有参数和返回值;
- 函数对象超出普通函数的概念,函数对象可以有自己的状态;
- 函数对象可以作为参数传递。
谓词
谓词是返回值类型为bool的函数对象。如果operator()
接收一个参数,叫一元谓词;如果operator()
接收两个参数,叫二元谓词。
一元谓词
//一元谓词
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 + 1);
}
//fand_if,找到返回该元素的迭代器,否则返回结束迭代器
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
if (pos == v.end())
{
cout << "没有找到大于5的数字" << endl;
}
else
{
cout << "第一个大于5的数字为:" << *pos << endl;
}
}
这个案例里面用到了一个标准算法find_if
,它会按条件查找满足要求的元素。这里说的“要求”就是仿函数的功能。在本例中,仿函数是判断一个数字是否比5大,如果大于5,则返回真,否则返回假。此仿函数作为参数传递给find_if
,就可以遍历查找比五大的数字了。如果查到了,返回该元素所在的迭代器,否则返回end迭代器。
二元谓词
//二元谓词
class Compare
{
public:
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
void printVector(const vector<int> &v)
{
for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int> v;
v.push_back(2);
v.push_back(3);
v.push_back(5);
v.push_back(4);
v.push_back(1);
sort(v.begin(), v.end()); //从小到大排序
printVector(v); //1 2 3 4 5
//使用函数对象改算法策略为降序排列
sort(v.begin(), v.end(), Compare());
cout << "降序排列" << endl;
printVector(v);// 5 4 3 2 1
}
标准算法sort
默认情况下是按声升序排列的,但是我们可以通过二元谓词来改变它的排序策略为降序。