内建函数对象

内建函数对象意义

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;
}
相关推荐
zzz9337 分钟前
transformer实战——mask
算法
INS_KF27 分钟前
【C++知识杂记2】free和delete区别
c++·笔记·学习
一只鱼^_35 分钟前
牛客周赛 Round 105
数据结构·c++·算法·均值算法·逻辑回归·动态规划·启发式算法
是阿建吖!36 分钟前
【动态规划】斐波那契数列模型
算法·动态规划
ikkkkkkkl37 分钟前
C++设计模式:面向对象设计原则
c++·设计模式·面向对象
啊阿狸不会拉杆1 小时前
《算法导论》第 27 章 - 多线程算法
java·jvm·c++·算法·图论
重启的码农1 小时前
ggml介绍 (8) 图分配器 (ggml_gallocr)
c++·人工智能·神经网络
火车叨位去19491 小时前
力扣top100(day04-05)--堆
算法·leetcode·职场和发展
数据智能老司机1 小时前
面向企业的图学习扩展——面向图的传统机器学习
算法·机器学习
重启的码农1 小时前
ggml介绍 (9) 后端调度器 (ggml_backend_sched)
c++·人工智能·神经网络