c++ 谓词

 

1. 一元谓词

#include 
#include
#include

using namespace std;

class CreaterFive{
public:
    bool operator()(int val){
        return val>5;
    }
};


int main()
{
   vector vec;
   for(int i=0; i<=10; ++i){
       vec.push_back(i);
   }
   vector::iterator it = find_if(vec.begin(), vec.end(), CreaterFive());
   if(it==vec.end()){
       cout << "未找到" << endl;
   }
   else{
       cout << "找到,为:"<< *it << endl;
   }
   
   return 0;
}

输出:

c++ 谓词_第1张图片

 

2. 二元谓词

#include 
#include
#include

using namespace std;



class MyCompare{
public:
    bool operator()(int a, int b){
        return a > b;
    }
};

int main()
{
   vector vec;
   vec.push_back(10);
   vec.push_back(30);
   vec.push_back(50);
   vec.push_back(100);
   vec.push_back(60);
   
   sort(vec.begin(), vec.end(), MyCompare());    //利用二元谓词修改排序方式
   for(vector::iterator it=vec.begin(); it

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