c++函数对象(仿函数)、谓词、内建函数对象

1、函数对象

1.1 概念

        重载函数调用操作符的类,这个类的对象就是函数对象,在使用这个函数对象对应使用重载的()符号时,行为类似于函数调用,因此这个函数也叫仿函数。

注意:函数对象(仿函数)是一个类,不是函数。

        特点:

        a)函数对象可以传入参数、返回值

        b)可以使用类的成员参数记录函数对象的状态

        c)函数对象可以作为参数传递。

1.2 示例

        特点a、b使用:

//创建一个函数对象,
class Print {
public:
	void operator()(string &tex) {
		cout << tex << endl;
		pNum++;
	}

	int pNum{};
};


int main() {

	string word{ "speak sth!" };
	Print print;
	print(word);
	print(word);
	print(word);
	print(word);
	cout << "" << print.pNum << endl;;

	system("pause");
	return 0;
}

        特点c:

class mData {
public:
	mData(int x,float y) {
		this->mX = x;
		this->mY = y;
	}
	~mData(){}
	int mX{};
	float mY{};
};

class Sort {
public:
	bool operator()(mData& md1, mData& md2) {
		return md1.mX > md2.mX;
	}
};
void func(mData& md1, mData& md2) {
	Sort st;
	if (st(md1, md2)) {
		cout << " a > b" << endl;
	}
	else {
		cout << " a < b" << endl;
	}
}
int main() {

	vector vP{};
	vP.push_back(mData(1, 2.));
	vP.push_back(mData(4, 12.));

	func(vP[0], vP[1]);

	system("pause");
	return 0;
}

2、谓词

2.1 概念

        返回bool类型的仿函数为谓词,()内有一个参数为一元谓词、有两个参数为二元谓词。

2.2示例

        一元谓词:

//一元谓词创建:一元谓词bool返回值,一个传入参数
class findPass7 {
public:
	bool operator()(int val) {
		return val > 5;
	}
};
int main() {

	vector v1;

	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	vector::iterator iter = find_if(v1.begin(), v1.end(), findPass7());
	if (iter != v1.end()) {
		cout << "找到5,位于:" << (*iter) << endl;
	}
	else {
		cout << "没找到"<< endl;
	}
	system("pause");
	return 0;
}

注:如果想在stl中使用自定义的数据,类型,首先看这个函数有没有相关的重载,如下函数最右边的 _Pred

        使用二元谓词实现对自定义类的成员参数排序

class Person {
public:
	Person(int age, int length) {
		this->mAge = age;
		this->mLength = length;
	}
	int mAge;
	int mLength;
};
class getAgeMid {
public:
	bool operator()(Person& p1, Person& p2) {
		return p1.mAge > p2.mAge;
	}
};
int main() {

	vector Ps;
	Ps.push_back(Person(10,23));
	Ps.push_back(Person(20, 33));
	Ps.push_back(Person(50, 13));
	Ps.push_back(Person(40, 53));
	Ps.push_back(Person(30, 73));

	sort(Ps.begin(), Ps.end(), getAgeMid());

	for (vector::iterator iter = Ps.begin(); iter != Ps.end(); iter++) {
		cout << (*iter).mAge << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3、stl内建函数对象 

3.1 分类与用法

        算术仿函数、关系仿函数、逻辑仿函数。

        引入头文件 : #include 即可使用

 3.2 算术仿函数

        功能:实现四则运算,negate是一元运算,其余都为二元运算。

        仿函数原型:

        c++函数对象(仿函数)、谓词、内建函数对象_第1张图片

	//初始化算术仿函数
	plus p;
	negate ng;
	p(a, b);

	ng(79);

3.3 关系仿函数

        功能:实现关系对比

        仿函数原型:

        c++函数对象(仿函数)、谓词、内建函数对象_第2张图片

         利用内建仿函数实现降序排序:

int main() {

	vector v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(50);
	v1.push_back(30);
	v1.push_back(40);

	for (vector::iterator iter = v1.begin(); iter != v1.end(); iter++) {
		cout << (*iter) << " ";
	}
	cout << endl;
	sort(v1.begin(), v1.end(), greater());

	for (vector::iterator iter = v1.begin(); iter != v1.end(); iter++) {
		cout << (*iter) << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.4 逻辑仿函数

        功能:实现逻辑运算

        仿函数原型:

        c++函数对象(仿函数)、谓词、内建函数对象_第3张图片

 

你可能感兴趣的:(c++学习笔记,c++,开发语言)