爱上c++的第十四天:STL-函数对象

152 阅读3分钟

你的c++学习路上明灯_哔哩哔哩_bilibili

上述是我在B站发布的相应视频

目录

函数对象

一,概念

二,本质

三,特点

谓词

一,概念

内建函数对象

一,概念

二,分类

三,用法

四,实例

1,算术运算符

2,关系仿函数

3,逻辑仿函数

函数对象

一,概念

1,重载函数调用操作符的类,其对象称为函数对象。

2,函数对象使用重载的()时,行为类似于函数调用,也叫仿函数。

二,本质

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

三,特点

#include<iostream>
#include<string>
using namespace std;

class MyAdd {
public:
	int operator()(int a, int b) {
		return a + b;
	}

	int count = 0;
};

void test1() {
	MyAdd myadd;

	cout << myadd(29, 30) << endl;
	
}

class Myprint {
public:
	void operator()(string test) {
		cout << test << endl;
		this->count++;
	}

	int count = 0;
};

void doPrint(Myprint& my,string test) {
	my(test);
}
void test2() {
	Myprint my;
	my("hello world");
	my("hello world");
	my("hello world");

	cout << my.count << endl;
}

void test3() {
	Myprint my;
	doPrint(my, "hello");

}

int main() {
	//test1();

	//test2();
	
	test3();

	return 0;
}

谓词

一,概念

1,返回布尔类型的仿函数称为谓词

2,如果operator()接收一个参数,就叫做一元谓词

3,如果operator()接收两个参数,就叫做二元谓词

一元谓词

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class func {
public:
	bool operator()(int val) {
		return val > 5;
	}
};

void test1() {
	vector<int>v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}

	vector<int>::iterator it=find_if(v.begin(), v.end(), func());
	if (it == v.end()) {
		cout << "未找到" << endl;
	}
	else {
		cout << "已找到" << endl;
		cout << *it << endl;
	}

}

int main() {
	test1();

	return 0;
}

二元谓词

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class MyCompare {
public:
	bool operator()(int val1,int val2) {
		return val1 > val2;
	}
};

void test1() {
	vector<int>v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(120);
	v.push_back(20);

	sort(v.begin(), v.end());

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
    
	cout << "------------------" << endl;

	sort(v.begin(), v.end(),MyCompare());

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	
}

int main() {
	test1();

	return 0;
}

内建函数对象

一,概念

STL提供的一些函数对象

二,分类

1,算术仿函数

2,关系仿函数

3,逻辑仿函数

三,用法

1,这些仿函数所产生的对象,用法和一般函数完全相同

2,使用内建函数对象,需要引入头文件

四,实例

1,算术运算符

#include<iostream>
#include<functional>
using namespace std;

void test1() {
	//一元运算
	negate<int>n;
	cout << n(20) << endl;
}

void test2() {
	//二元运算
	//默认为相同数据类型的运算
	plus<int>p;
	cout << p(10, 20) << endl;

}

int main() {
	test1();
	test2();

	return 0;
}

2,关系仿函数

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;

class MyCompare {
public:
	bool operator()(int val1,int val2) {
		return val1 > val2;
	}

};

void test1() {
	vector<int>v;
	v.push_back(10);
	v.push_back(210);
	v.push_back(30);
	v.push_back(50);

	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(),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() {
	test1();

	return 0;
}

3,逻辑仿函数

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

void test1() {
	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;

	//利用逻辑非,取反存储到另一个vector容器中
	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;
	cout << endl;
}

int main() {
	test1();

	return 0;
}