函数对象

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

#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))

你可能感兴趣的:(c,String,iterator)