内建函数对象

内建函数对象意义

STL内建了一些函数对象

分类:

算术仿函数

关系仿函数

逻辑仿函数

用法:

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

使用内建函数对象,需要引入头文件#include<functional>

算术仿函数

功能描述:

实现四则运算

其中negate是一元运算,其他都是二元运算

仿函数原型:

template<class T> T plus<T>//加法仿函数

template<class T> T minus<T>//减法仿函数

template<class T> T multiplies<T>//乘法仿函数

emplate<class T> T divides<T>//除法仿函数t

template<class T> T modulus<T>//取模仿函数

template<class T> T negate<T>//取反仿函数

示例

cpp 复制代码
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>

using namespace std;

void test01()
{
    negate<int>n;

	cout << n(50) << endl;//-50
}

void test02()
{
	plus<int>p;
	
	cout << p(10,20)<< endl;//30
}

int main()
{
	test01();
	test02();

    return 0;
}

关系仿函数

功能描述:

实现关系对比

仿函数原型:

template<class T> bool equal to<T>//等于

template<class T> bool not equal to<T>//不等于

template<class T> bool greater<T>//大于

template<class T> bool greater equal<T>//大于等于

template<class T> bool less<T>//小于

template<class T> bool less-equal<T>//小于等于

cpp 复制代码
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>

using namespace std;
 
class mycom
{
public:
	int operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
 
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);	
	for(vector<int>::iterator it = v.begin();it!=v.end();it++)
	{
		cout << *it;
	}
	cout << endl;
	//sort(v.begin(),v.end(),mycom());
	sort(v.begin(),v.end(),greater<int>());//编译器提供的函数对象
	for(vector<int>::iterator it = v.begin();it!=v.end();it++)
	{
		cout << *it;
	}
	
}

int main()
{
	test01();

    return 0;
}

逻辑仿函数

功能描述:

实现逻辑运算

函数原型:

template<class T> bool logical and<T>//逻辑与

template<class T> bool logical or<T> //逻辑或

template<class T> bool logical not<T>//逻辑非

cpp 复制代码
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include<functional>

using namespace std;
 
class mycom
{
public:
	int operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
 
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;          //1 0 1 0 
	//利用逻辑非 将容器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;          //0 1 0 1 

	
}

int main()
{
	test01();

    return 0;
}
相关推荐
朝朝又沐沐3 小时前
算法竞赛阶段二-数据结构(36)数据结构双向链表模拟实现
开发语言·数据结构·c++·算法·链表
薰衣草23334 小时前
一天两道力扣(6)
算法·leetcode
逝雪Yuki4 小时前
Leetcode——287. 寻找重复数
c++·leetcode·二分查找·双指针·环形链表
剪一朵云爱着4 小时前
力扣946. 验证栈序列
算法·
遇见尚硅谷4 小时前
C语言:*p++与p++有何区别
c语言·开发语言·笔记·学习·算法
天天开心(∩_∩)5 小时前
代码随想录算法训练营第三十二天
算法
YouQian7725 小时前
(AC)缓存系统
算法·缓存
艾莉丝努力练剑5 小时前
【数据结构与算法】数据结构初阶:详解排序(二)——交换排序中的快速排序
c语言·开发语言·数据结构·学习·算法·链表·排序算法
科大饭桶5 小时前
数据结构自学Day13 -- 快速排序--“前后指针法”
数据结构·算法·leetcode·排序算法·c
李永奉5 小时前
C语言-流程控制语句:for循环语句、while和do…while循环语句;
c语言·开发语言·c++·算法