C++——STL(仿函数、谓词、内建函数对象)

2021/6/20 17:22:23

本文主要是介绍C++——STL(仿函数、谓词、内建函数对象),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

函数对象:
重载函数调用操作符的类,其对象常称为函数对象;
函数对象使用重载的()时,行为类似函数调用,也叫仿函数;
本质:
函数对象(仿函数)是一个类,不是一个函数;

函数对象使用:
特点:
函数对象在使用时。可以像普通函数那样调用,可以有参数,可以有返回值;
函数对象超出普通函数的概念,函数对象可以有自己的状态;
函数对象可以作为参数传参;

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

class MYADD {
public:

	int operator()(int data1, int data2) {
		return data1 + data2;
	}
};


class MYPRINT {
public:

	int count = 0;      //自己的状态

	void operator()(string text) {
		cout << text << endl;

		count++;
	}
};

void test()
{
	MYADD myadd;
	cout << myadd(10, 10) << endl;

}

void test02()
{
	MYPRINT myprint;
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");

	cout << "调用仿函数的次数:" << myprint.count << endl;
}

void doprint(MYPRINT &print, string text)     //仿函数对象可以作为参数进行传递
{
	print(text);
}

void test03()
{
	MYPRINT p1;
	string s1 = "text";
	doprint(p1, s1);
}


int main()
{
	test();
	test02();
	test03();
	system("pause");
	return 0;
}

谓词的概念:
返回bool类型的仿函数称为谓词;
如果operator()接受一个参数,那么叫做一元谓词;
如果operator()接受两个参数,那么叫做二元谓词;

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

算术仿函数:
实现四则运算
其中negate是一元运算,其他都是二元运算

template T plus //加法仿函数
template T minus //减法仿函数
template T multiplies //乘法仿函数
template T divides //除法仿函数
template T modulus //取模仿函数
template T negate //取反仿函数

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

//negate  一元取反仿函数
void test()
{
	negate<int> n1;
	cout << n1(10) << endl;
}


void test02()
{
	plus<int>p1;    //只需要传一个int
	cout << p1(10, 20) << endl;
}

int main()
{
	test();
	test02();
	system("pause");
	return 0;
}

关系仿函数:
实现关系对比;

template bool equal_to //等于
template bool not_equal_to //不等于
template bool greater //大于
template bool greater_equal //大于等于
template bool less //小于
template bool less_equal //小于等于

利用关系仿函数great实现vector降序排列;

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

void test()
{
	vector<int>v1;
	v1.push_back(30);
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(60);
	v1.push_back(21);
	v1.push_back(32);

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

	sort(v1.begin(), v1.end(), greater<int>());

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

	cout << endl;
}

int main()
{
	test();

	system("pause");
	return 0;
	
}

逻辑仿函数:
实现逻辑运算;

template bool logical_and //逻辑与
template bool logical_or //逻辑或
template bool logical_not //逻辑非

#include <vector>
#include <functional>
#include <algorithm>
void test01()
{
	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() {

	test01();

	system("pause");

	return 0;
}

learned from:黑马程序员



这篇关于C++——STL(仿函数、谓词、内建函数对象)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程