函数对象

97 篇文章 0 订阅
92 篇文章 0 订阅

函数对象,一个类里面定义了一个操作符重载()操作,所以使用起来像函数

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class GTF{
public:
 GTF(int size){
  this->size=size;
 }
 bool operator()(const int aa){
  return this->size<aa;
 }
private:
 int size;
};
int main(){
 int a[]={1,2,3,4,5,6,7,8,9,10};
 vector<int>v(a,a+10);
 for(vector<int>::iterator iter=v.begin();iter!=v.end();++iter){
  cout<<*iter<<" ";
 }
 cout<<endl;
 cout<<"********************************"<<endl;
 vector<int>::iterator first=find_if(v.begin(),v.end(),GTF(5));
 cout<<*first<<endl;
 cout<<endl;
 system("pause");
 return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class RP{
public:
 RP(string s){
  this->word=s;
 }
 bool operator()(string x){
  return this->word==x;
 }
private:
 string word;
};

int main(){
 string s;
 vector<string>v;
 cout<<"input some words"<<endl;
 while(cin>>s){
  v.push_back(s);
 }
 cout<<"befor replace:"<<endl;
 for(vector<string>::iterator iter=v.begin();iter!=v.end();++iter){
  cout<<*iter<<" ";
 }
 cout<<endl;
 string oldword;
 cout<<"input the old word you want to replace"<<endl;
 cin.clear();
 cin>>oldword;
 cout<<"input the new word you want to replace"<<endl;
 string newword;
 cin>>newword;
 cout<<"after replace"<<endl;
 replace_if(v.begin(),v.end(),RP(oldword),newword);
 for(vector<string>::iterator iiter=v.begin();iiter!=v.end();++iiter){
  cout<<*iiter<<" ";
 }
 cout<<endl;
 system("pause");
 return 0;
}

 标准库定义的函数对象 functional头文件

#include<iostream>
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main(){
 plus<int>add;
 int x=add(10000,86);
 cout<<x<<endl;
 cout<<"**************************"<<endl;
 string s[]={"C","E","D","F","H","Q","W","X","Z","T"};
 vector<string>v(s,s+10);
 for(vector<string>::iterator beg1=v.begin();beg1!=v.end();++beg1)
 {
  cout<<*beg1<<" ";
 }
 cout<<endl;

 sort(v.begin(),v.end());
 for(vector<string>::iterator beg2=v.begin();beg2!=v.end();++beg2)
 {
  cout<<*beg2<<" ";
 }
 cout<<endl;

 sort(v.begin(),v.end(),greater<string>());
 for(vector<string>::iterator beg3=v.begin();beg3!=v.end();++beg3)
 {
  cout<<*beg3<<" ";
 }
 cout<<endl;
 sort(v.begin(),v.end(),less<string>());
 for(vector<string>::iterator beg4=v.begin();beg4!=v.end();++beg4)
 {
  cout<<*beg4<<" ";
 }
 cout<<endl;
 system("pause");
 return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
bool GT(const int a){
 return  a>100;
}
int main(){
 int a[]={21,45,78,62,48,156,487,15,12,36};
 vector<int>v(a,a+10);
 
 vector<int>::iterator flag=v.begin();
 while((flag=find_if(flag,v.end(),GT))!=v.end()){
  cout<<*flag<<endl;
  ++flag;
 }

 system("pause");
 return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main(){
 int a[]={21,45,78,62,48,156,487,15,12,36};
 vector<int>v(a,a+10);
 int count=count_if(v.begin(),v.end(),bind2nd(less_equal<int>(),20));
 cout<<count<<endl;//小于等于20的个数
 count=count_if(v.begin(),v.end(),not1(bind2nd(less_equal<int>(),20)));
 cout<<count<<endl;//大于20的个数
 
 vector<int>::iterator flag=v.begin();
 while((flag=find_if(flag,v.end(),bind2nd(less_equal<int>(),20)))!=v.end()){
  cout<<*flag<<" ";//输出小于等于20的数
  ++flag;
 }
 cout<<endl;
 vector<int>::iterator flag2=v.begin();
 while((flag2=find_if(flag2,v.end(),not1(bind2nd(less_equal<int>(),20))))!=v.end()){
  cout<<*flag2<<" ";//输出大于20的数
  ++flag2;
 }
 
 cout<<endl;
 system("pause");
 return 0;
}

 

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
int main(){
 string s;
 vector<string>v;
 while(cin>>s){
  v.push_back(s);
 }
 cout<<"input the word  you don't want to see"<<endl;
 cin.clear();
 string word;
 cin>>word;
 vector<string>::iterator flag=v.begin();
 while((flag=find_if(flag,v.end(),bind2nd(not_equal_to<string>(),"pooh")))!=v.end()){
  cout<<*flag<<" ";
  ++flag;
 }

 system("pause");
 return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
 int a[]={1,2,3,4,5,6,7,8,9,10};
 vector<int>v(a,a+10);

 transform(v.begin(),v.end(),v.begin(),bind2nd(multiplies<int>(),2));
 for(vector<int>::iterator iter=v.begin();iter!=v.end();++iter){
  cout<<*iter<<" ";
 }
 cout<<endl;
 system("pause");
 return 0;
}

 

#include<iostream>
#include<fstream>
#include<string>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
 ifstream findtxt("c:\\hello.txt");
 if(!findtxt){
  cout<<"can't open it"<<endl;
 }
 vector<string>v;
 string word;
 while(findtxt>>word){
  v.push_back(word);
 }
 sort(v.begin(),v.end());

 vector<string>::iterator uni=unique(v.begin(),v.end());

 v.erase(uni,v.end());

 vector<string>::size_type t=0;
 greater_equal<string::size_type>grep;
 for(vector<string>::iterator iter=v.begin();iter!=v.end();++iter){
  if(grep((*iter).size(),10)){
   ++t;
  }
 }
 cout<<t<<endl;
 system("pause");
 return 0;
}

 

plus<type>

greater<type>

less<type>

divides<type>

negate<type>

函数对象的函数适配器

绑定器 bind1st(),bind2nd(less_equal<int>(),20)

求反器not1 not2  not1(bind2nd(less_equal<int>(),20))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值