【C++中的STL】函数对象

函数对象

  • 函数对象概念
  • 谓词
    • 概念
  • 内建函数对象
    • 算术仿函数
    • 关系仿函数
    • 逻辑仿函数(基本用不到)

函数对象概念

重载函数调用操作符的类,其对象常称为函数对象,函数对象使用重载的()时。行为类似函数调用,也叫仿函数。
函数对象(仿函数)是一个类,不是一个函数。
使用特点:
函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值。函数对象超出普通函数的概念,函数对象可以有自己的状态,函数对象可以作为参数传递。

//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值。
class myAdd {
public:
	int operator()(int a, int b) {
		return a + b;
	}
};
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class B {
public:
	int count;//内部自己的状态
	B(){
		count = 0;
	}
	void operator()(string s) {
		cout << s << endl;
		this->count++;
	}

};
//函数对象可以作为参数传递
void doP(B& b1, string b2) {
	b1(b2);
}
void test() {
	myAdd ma;
	cout << ma(10, 10) << endl;
	B b2;
	b2("dasfafasfg");
	b2("dasfafasfg");
	b2("dasfafasfg");
	b2("dasfafasfg");
	cout << "次数:" << b2.count << endl;

	B b3;
	doP(b3, "C++");
}

【C++中的STL】函数对象_第1张图片

谓词

概念

返回bool类型的仿函数称为谓词,如果operator()接受一个参数,叫做一元谓词;如果operator()接受两个参数,叫做二元谓词。
一元谓词:
【C++中的STL】函数对象_第2张图片

class CreateN {
public:
	bool operator()(int val) {
		return val > 5;
	}
};
void test() {
	vector<int> v;
	for (int i = 0;i < 5;i++) {
		v.push_back(i);
	}
	//查找容器中有没有大于5的数字,CreateN()匿名函数对象
	vector<int>::iterator it=find_if(v.begin(), v.end(), CreateN());
	if (it == v.end()) {
		cout << "没找到" << endl;
	}
	else {
		cout << "找到" << endl;
	}
}

二元谓词

class myCom {
public:
	bool operator()(int v, int v1) {
		return v > v1;
	}
};
void test() {
	vector<int> v;
	v.push_back(10);
	v.push_back(24);
	v.push_back(12);
	v.push_back(35);
	v.push_back(20);
	sort(v.begin(),v.end());
	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
		cout << *it << endl;
	}
	cout << "---------------------" << endl;
	//使用函数对象改变算法规则,从大到小
	sort(v.begin(), v.end(),myCom());
	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
		cout << *it << endl;
	}
}

【C++中的STL】函数对象_第3张图片

内建函数对象

STL内建了一些函数对象,有算术仿函数、关系仿函数、逻辑仿函数,这些仿函数所产生的对象,用法和一般函数完全相同,使用内建函数对象,需要引入头文件**#include**

算术仿函数

实现四则运算,其中negate是一元运算,其他都是二元运算。
仿函数原型

1、template T plus < T >加法仿函数
2、template T minus < T >减法仿函数
3、template T multiplies < T >乘法仿函数
4、template T divides < T >除法仿函数
5、template T modulus < T >取模仿函数
6、template T negate < T >取反仿函数

void test() {
	//取反仿函数(一元用法)
	negate<int> n;
	cout << n(50) << endl;
	//加法仿函数(二元用法)
	plus<int> p;
	cout << p(10, 34) << endl;
}

关系仿函数

实现关系对比
仿函数原型
1、template T bool equal_to< T >等于
2、template T bool not_equal_to< T >不等于
3、template T bool greater< T >大于(最常用)
4、template T bool greater_equal< T >大于等于
5、template T bool less< T >小于
6、template T bool less_equal< T >小于等于

void test() {
	...
	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
		cout << *it << "   ";
	}
	cout << endl;
	//降序  greater()内建函数对象,和自己实现的仿函数效果一样
	sort(v.begin(), v.end(), greater<int>());
}

逻辑仿函数(基本用不到)

1、template T bool logical_and< T >逻辑与
2、template T bool logical_or< T >逻辑或
3、template T bool logical_not< T >逻辑非

void test() {
	....
	cout << endl;
	cout << "-----------------------------------------" << endl;
	//利用逻辑非,将容器v搬运到容器v1中,并执行取反操作
	vector<bool>v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(), v1.begin(),logical_not<bool>());//搬运,必须要开辟不骗空间才可使用
	for (vector<bool>::iterator it = v1.begin();it != v1.end();it++) {
		cout << *it << "   ";
	}
}

你可能感兴趣的:(C++,c++,算法)