一元谓词案例与二元谓词案例
来源:互联网 发布:全面战争系列优化渣 编辑:程序博客网 时间: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");}
阅读全文
0 0
- 一元谓词案例与二元谓词案例
- 自定义一元谓词和二元谓词以及使用方法
- 二元谓词的重载
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 谓词
- 二元谓词的重载2
- 一元函数对象和一元谓词
- 二元函数对象和二元谓词
- # 如何在linux系统上下载和安装运行wps组件
- C语言的32个关键字(1)
- 1台电脑可以上网,通过网络共享,让另外一台电脑也可以上网
- 我觉得,我是该净化一下我的心灵了
- MyEclipse结合GitHub简单实用教程
- 一元谓词案例与二元谓词案例
- rollup,支持多种打包方式的前端利器
- @Controller和@RestController的区别?
- 新手安装Ubuntu 16.04 操作系统
- AndroidStudio嵌入ReactNative混合开发环境搭建
- ROC
- php编程文件上传代码
- HDOJ-1002 A + B Problem II 高精度(java)
- Roman to Integer