zoukankan      html  css  js  c++  java
  • 判断式

    判断式是一个返回boolean值的韩叔叔,通常被用来指定排序准则和搜索准则。

    一 Unary Predicates(一元判断式)

    检查唯一参数的某项特性:

    如:搜索第一个质数:

    #include<iostream>

    #include<list>

    #include<algorithm>

    #include<cstdlib>

    using namespace std;

    bool isPrime(int number)

    {

    //ignore negative sign

    number = abs(number);

    //0 and 1 are prime numbers

    if (number==0 || number==1)

    {

    return true;

    }

    int divisor;

    for(divisor=number/2;number%divisor !=0;--divisor);

    return divisor == 1;

    }

    int main()

    {

    list<int>coll;

    //insert element from 24 to 50

    for(int i=24;i<50;++i)

    {

    coll.push_back(i);

    }

    //search for prime number

    list<int>::iterator pos;

    pos=find_if(coll.begin(),coll.end(),//range

    isPrime);//predicate

    if(pos !=coll.end())

    cout<<*pos<<"    is first prime number found"<<endl;

    else

    {

    cout<<"no prime number found"<<endl;

    }

    return 0;

    }

    输出:

    wps_clip_image-27561

    Binary Predicates

    比较两个参数的特定属性

    #include<iostream>

    #include<list>

    #include<algorithm>

    #include<cstdlib>

    #include<deque>

    using namespace std;

    class Person

    {

    public:

        Person();

        ~Person();

    public:

        string firstname()const;

        string lastname()const;

    };

    bool PersonSortCriterion(const Person&p1,const Person&p2)

        {

            return p1.firstname<p2.firstname ||

                (!(p2.lastname<p1.lastname) &&

                p1.firstname<p2.firstname);

        }

    int main()

    {

    deque<Person>coll;

    sort(coll.begin(),coll.end(),

    PersonSortCriterion);

    }

  • 相关阅读:
    Linux_LEMP
    Linux_LEMP
    Linux_指令杂烩
    Linux_指令杂烩
    Linux_SELinux使用
    AWS S3存储基于Hadoop之上的一致性保证
    Ozone数据写入过程分析
    Ozone Datanode的分布式元数据管理
    聊聊Ozone的Topology Awareness
    Ozone数据探查服务Recon的启用
  • 原文地址:https://www.cnblogs.com/bacazy/p/3267059.html
Copyright © 2011-2022 走看看