STL中的二元函数对象和二元谓词

本文探讨了STL中的二元函数对象,这类对象具有两个输入参数并能进行相应操作。同时,介绍了二元谓词的概念,它也是接受两个参数,但返回值为布尔类型。通过示例代码,读者可以更深入理解这两个概念。
摘要由CSDN通过智能技术生成

二元函数对象:重载运算符()的函数有两个参数

二元谓词:函数有两个输入参数,并且返回值为bool类型

 

具体的可以看如下代码:

#include<iostream>
using namespace std;
#include "string"
#include <vector>
#include <list>
#include "set"
#include <algorithm>
#include "functional"

//二元函数对象
template<typename T>
class SumAdd
{
public:
	T operator()(T& t1, T& t2)
	{
		return t1 + t2;
	}
};


void main04()
{
	vector<int> v1, v2;
	vector<int> v3;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);

	v2.push_back(2);
	v2.push_back(4);
	v2.push_back(6);

	v3.resize(10);

	transform(v1.begin(),v1.end(),v2.begin(),v3.begin(), SumAdd<int>());

	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout << *it << " ";   //输出3  7  11  0  0  0  0  0  0  0
	}
	cout << endl;
}


//二元谓词
template<typename T>
bool MyCompare
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值