内建函数对象

内建函数对象意义

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;
}
相关推荐
雾月559 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
OpenC++38 分钟前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
YuforiaCode1 小时前
第十二届蓝桥杯 2021 C/C++组 直线
c语言·c++·蓝桥杯
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
草莓啵啵~2 小时前
搜索二叉树-key的搜索模型
数据结构·c++
共享家95272 小时前
深入理解C++ 中的list容器
c++
孞㐑¥2 小时前
C++11介绍
开发语言·c++·经验分享·笔记
云小逸2 小时前
【QQMusic项目界面开发复习笔记】第二章
c++·qt