一元谓词案例与二元谓词案例

来源:互联网 发布:全面战争系列优化渣 编辑:程序博客网 时间:2024/06/05 05:28

谓词

一元谓词: 函数参数1个,函数返回值是bool类型,可以作为一个判断式

二元谓词: 函数参数2个,函数返回值是bool类型

谓词可以使一个仿函数,也可以是一个回调函数。

一元谓词案例

求0到500整数哪些数被19整除

#include <iostream>using namespace std;#include <queue>#include<vector>template <typename T>class Isdiv{public:    Isdiv(const T &divisor) //    {        this->divisor = divisor;    }    bool operator()(T &t)   //1元谓词 例子    {        return (t%divisor == 0);    }protected:private:    T divisor;};void main(){    vector<int> v2;    for (int i = 1; i<500; i++)    {        v2.push_back(i);    }    vector<int>::iterator it;    int a = 4;    Isdiv<int> mydiv(4);    // _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)   //返回的是迭代器    int cn = 0;    it = find_if(v2.begin(), v2.end(), Isdiv<int> (19));    while(it != v2.end())    {        cn++;        cout << "第"<<cn<<"个被4整除的数是:" << *it << endl;        it = find_if(it+1, v2.end(), Isdiv<int>(19));    }    cout << "一共有" << cn << "个数被19整除" << endl;    system("pause");}

这里写图片描述

二元谓词

//1个key值可以对应多个valude(分组) //公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) //人员信息有:姓名,年龄,电话、工资等组成//通过 multimap进行 信息的插入、保存、显示//分部门显示员工信息#include <iostream>#include <string>#include <map>using namespace std;class Person{public:    Person(string name,int age)    {        m_name = name;        m_age = age;    }    string m_name;    int m_age;protected:private:};int main(void){    Person p1("张三",32),p2("李四",33),p3("王二",34),p4("麻子",35),p5("赵五",36);    multimap<string, Person> map;    map.insert(make_pair("Sale", p1));    map.insert(make_pair("Sale", p2));    map.insert(make_pair("Development", p3));    map.insert(make_pair("Development", p4));    map.insert(make_pair("Financial", p5));    for (multimap<string, Person>::iterator it = map.begin(); it != map.end();it++)    {        cout << it->first << "\t" << (it->second).m_name <<endl;    }    int num = map.count("Development");    cout << "开发部的人数:" << num << endl;    cout << "开发部的员工信息:" << endl;    multimap<string, Person>::iterator new_it = map.begin();    while (new_it != map.end() )    {        if (new_it->first == "Development")        {            new_it->second.m_age = 18;            cout << new_it->first << "\t" << (new_it->second).m_name << "\t" << (new_it->second).m_age << endl;        }          new_it++;     }    cout<<"Hello!"<<endl;    system("pause");    return 0;}

这里写图片描述

#include <iostream>using namespace std;#include <queue>#include<vector>#include<set>struct CompareNoCase{    bool operator()(const string &str1, const string &str2) const//在调用operator()的时候要求operator()也具有const属性,否则就会导致丢失const限定符的错误    {        string str1_;        str1_.resize(str1.size());        transform(str1.begin(), str1.end(), str1_.begin(), tolower); //预定义函数对象         string str2_;        str2_.resize(str2.size());        transform(str2.begin(), str2.end(), str2_.begin(), tolower); //预定义函数对象         return (str1_ < str2_); // 从小到大进行排序    }};void  main(){    set<string> set1;    set1.insert("bbb");    set1.insert("aaa");    set1.insert("ccc");    set<string>::iterator it = set1.find("aAa"); //find函数 默认 区分大小写    if (it == set1.end())    {        cout << " 没有 查找到 aaa " << endl;    }    else    {        cout << " 查找到 aaa " << endl;    }    set<string, CompareNoCase> set2;    set2.insert("bbb");    set2.insert("aaa");    set2.insert("ccc");    set<string, CompareNoCase>::iterator it2 = set2.find("aAa");    if (it2 == set2.end())    {        cout << " 没有 查找到 aaa " << endl;    }    else    {        cout << " 不区分大小的的查找  查找到 aaa " << endl;    }    system("pause");}

这里写图片描述

原创粉丝点击