内建函数对象

内建函数对象意义

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;
}
相关推荐
阿昭L几秒前
c++中获取随机数
开发语言·c++
纪元A梦2 分钟前
贪心算法应用:基因编辑靶点选择问题详解
算法·贪心算法
3壹7 分钟前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
aaaweiaaaaaa34 分钟前
c++基础学习(学习蓝桥杯 ros2有C基础可看)
c++·学习·蓝桥杯·lambda·ros2·智能指针·c++类
skytier43 分钟前
Construct内报错和定位解决
算法
skytier1 小时前
Ascend print数据落盘使用
算法
一拳一个呆瓜1 小时前
【MFC】对话框属性:字体 (Font Name) 和 大小 (Font Size)
c++·mfc
etcix1 小时前
dmenux.c: integrate dmenu project as one file
c语言·前端·算法
papership1 小时前
【入门级-算法-6、排序算法:选择排序】
数据结构·算法·排序算法
郝学胜-神的一滴1 小时前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染