美文网首页
2.一元谓词

2.一元谓词

作者: lxr_ | 来源:发表于2021-04-26 10:13 被阅读0次
    #include<iostream>
    using namespace std;
    
    #include<vector>
    
    //返回bool类型的仿函数称为谓词
    //如果operator()接收一个参数,叫做一元谓词
    //如果operator()接收两个参数,叫做两元谓词
    
    class GreaterFive
    {
    public:
        bool operator()(int val)
        {
            return val > 5;
        }
    };
    void test0201()
    {
        vector<int> v;
        for (int i = 0; i < 10; i++)
        {
            v.push_back(i);
        }
    
        //查找容器中有没有大于5的数字
        //GreaterFive()为匿名函数对象
        vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());
    
        if (it == v.end())
        {
            cout << "未找到" << endl;
        }
        else
        {
            cout << "找到了大于5的数:" << (*it);
        }
        
    }
    
    int main()
    {
        test0201();
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:2.一元谓词

          本文链接:https://www.haomeiwen.com/subject/jcdjrltx.html