函数对象,一元谓词,二元谓词,预定义函数对象

find_if  ,  sort ,  for_each 的使用 for_each(v1.begin(), v1.end(), ShowElemt<int>() ) ;  要使用预定义函数对象需要包含 functional 头文件
vector<int>::iterator it = find_if(v1.begin(), v1.end(), myint )  sort(v3.begin(),v3.end(),Compare) ; // 按从大到小(递减)排序 
#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定义函数对象 (重载函数运算符的类)
template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

void Show(int & a)
{
	cout << a << " "  ;
}
int main(){
	ShowElem<int> showElemt ;
	vector<int> v1 ;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
showElemt = for_each(v1.begin(), v1.end(), showElemt ) ;
//showElemt = for_each(v1.begin(), v1.end(), ShowElemt<int>() ) ;
// 第三个参数为匿名函数对象,匿名仿函数  此处即可遍历输出。
// 函数对象能记录部分状态信息。  返回一个函数对象即可,再调用相应的函数输出状态。 
showElemt.printN();
for_each(v1.begin(), v1.end(),Show ) ;
} 
//ShowElem<int> showElemt ;
//showElemt(a)  函数对象的() 执行像一个函数//仿函数 

一元谓词:函数参数一个,函数返回值是bool类型 ; 二元谓词:函数参数二个,函数返回值是bool类型

find_if  返回的是一个迭代器。使用案例:

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
template <class T >
class Lv{
	public :
		Lv(const T & dis)
		{
			this->dis = dis ;
		}
		bool operator()(T &t){
			return (t%dis == 0) ;
		}
	protected :
	private :
		T dis ;
};
int main(){
	vector<int> v1 ;
	for( int i=1; i<10; ++i)
	{
		v1.push_back(i);
	}
	int a = 3 ;
	Lv<int> myint(a) ;
	vector<int>::iterator it = find_if(v1.begin(), v1.end(), myint ) ; // 返回的是迭代器 
	cout << *it ;
} // 程序输出的是 vector中第一个能被3整除的数。

二元谓词使用场景:可对两个对象进行操作,(+ - * / 或者是大小比较)

下面是二元函数对象的案例

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定义函数对象 

template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

template <class T >
class Sumadd{
	public :
		T operator()(T t1,T t2){
			return t1+t2 ;
		}
	protected :
	private :
		T dis ;
};

int main(){
	vector<int> v1 ;
	vector<int> v2 ;
	vector<int> v3 ;
	for( int i=1; i<=10; ++i)
	{
		v1.push_back(i);
		v2.push_back(i);
	}
	int a = 3 ;
	v3.resize(10) ;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Sumadd<int>( )) ; // 返回的是迭代器 
for_each(v3.begin(),v3.end(),ShowElem<int>()) ;
}
二元谓词的案例:
#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std ; 
//定义函数对象 

template <typename T>
class ShowElem {
	public:
		ShowElem(){
			n = 0 ;
		} 
		void operator()(T &t)
		{
			cout << t << " ";
			++ n ;
		}
		void printN(){
			cout <<"n " << n << " " << endl;
		}
	protected:
	private:
		int n ;
};

template <class T >
class Sumadd{
	public :
		T operator()(T t1,T t2){
			return t1+t2 ;
		}
	protected :
	private :
		T dis ;
};

bool Compare(const int &a , const int &b)
{
	return a > b ;
}
int main(){
	vector<int> v1 ;
	vector<int> v2 ;
	vector<int> v3 ;
	for( int i=1; i<=10; ++i)
	{
		v1.push_back(i);
		v2.push_back(i);
	}
	int a = 3 ;
	v3.resize(10) ;
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Sumadd<int>( )) ; // 返回的是迭代器 
sort(v3.begin(),v3.end(),Compare) ; // 按从大到小(递减)排序 
for_each(v3.begin(),v3.end(),ShowElem<int>()) ;
}

set.find 函数默认会区分大小写,要不区分大小写,可自己定义一个仿函数(函数对象) 然后在定义set时

set<int , ComapreNocase>  set1   ;  即可

struct CompareNocase{
	bool operator()(const string&str1 , congst string & str2){
		string str_1;
		str_1.resize(str1.size());
		transform(str1.begin(),str2.begin(),str_1.begin(), tolower) // tolower是预定于函数Udin对象 
			bool operator()(const string&str1 , congst string & str2){
		string str_2;
		str_2.resize(str2.size());
		transform(str2.begin(),str2.begin(),str_2.begin(), tolower) // tolower是预定于函数Udin对象
	}
}; 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值