内建函数对象

内建函数对象意义

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;
}
相关推荐
易只轻松熊9 分钟前
C++(21):fstream的读取和写入
开发语言·c++
小葡萄202514 分钟前
黑马程序员C++2024新版笔记 第三章 数组
笔记·算法·c++20
虾球xz1 小时前
游戏引擎学习第292天:实现蛇
c++·学习·游戏引擎
勇闯逆流河7 小时前
【数据结构】堆
c语言·数据结构·算法
jjkkzzzz7 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
pystraf7 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
Funny-Boy8 小时前
菱形继承原理
c++
飞川撸码8 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
yuhao__z8 小时前
代码随想录算法训练营第六十六天| 图论11—卡码网97. 小明逛公园,127. 骑士的攻击
算法
Echo``8 小时前
3:OpenCV—视频播放
图像处理·人工智能·opencv·算法·机器学习·视觉检测·音视频