预定义函数对象和函数适配器案例
来源:互联网 发布:aes加解密 java 编辑:程序博客网 时间:2024/05/22 00:11
#include <iostream> #include<string> #include <vector> #include <list> #include <set> #include <map> #include <algorithm> #include <functional> using namespace std; //函数对象class IsGreat{public:IsGreat(int n){this->n = n;}bool operator()(int i){if (i>n){return true;}return false;}protected:private:int n;};void display(){vector<int> v;int myarray[] = {10,20,20,20,30,40,50,60};for (int i =0;i<11;i++){v.push_back(i+1);}for (vector<int>::iterator it =v.begin();it!=v.end();it++){cout<<*it<<endl;}//统计 1在vector中出现的次数int num = count(v.begin(),v.end(),1);cout<<"num:"<<num<<endl;//统计数组myarray中20出现的次数int num2 = count(myarray,myarray+8,20);cout<<"num2:"<<num2<<endl;//通过谓词求vector大于3的个数int num3 = count_if(v.begin(),v.end(),IsGreat(3));cout<<"num3:"<<num3<<endl;//通过预定义函数对象 求大于3的个数/*//预定义函数对象greater 有两个参数 左参数来自容易的每一个元素 右参数通过bind2nd函数绑定2template <class T> struct greater : binary_function <T,T,bool> {bool operator() (const T& x, const T& y) const {return x>y;}};*/int num4 = count_if(v.begin(),v.end(),bind2nd(greater<int>(),2));cout<<"num4:"<<num4<<endl;//求奇数的个数 modulus/*//modulus 有两个参数 左操作数来自容器 右操作数来自bind2nd函数绑定的2template <class T> struct modulus : binary_function <T,T,T> {T operator() (const T& x, const T& y) const {return x%y;}};*/int num5 = count_if(v.begin(),v.end(),bind2nd(modulus<int>(),2));cout<<"num5:"<<num5<<endl;//求偶数的个数 modulusint num6 = count_if(v.begin(),v.end(),not1(bind2nd(modulus<int>(),2)));cout<<"num6:"<<num6<<endl;}int main(){display();system("pause");return 0;}
0 0
- 预定义函数对象和函数适配器案例
- stl算法设计理念_预定义函数对象和函数适配器2_案例
- stl算法设计理念_预定义函数对象和函数适配器1
- STL学习之十三:预定义函数和函数适配器
- 预定义函数对象
- STL与泛型编程<十五>:预定义的仿函数和仿函数适配器
- STL函数对象之预定义函数对象和函数对象绑定器
- 函数对象、函数适配器和算法
- 类 - 函数的预定义和调用
- 常用预定义函数
- Geogebra预定义函数
- 函数对象及适配器
- 函数对象适配器
- STL-函数对象适配器
- STL-函数对象适配器
- 函数对象的函数适配器
- 函数对象的函数适配器
- 函数对象的函数适配器
- secureCRT安装及破解
- 5min通过ViewPager实现简易微信主界面(1)-雏形
- 嵌入式系统学习(一)-S5P4418 芯片介绍
- Windows C++发送以太网(Ethernet)帧
- Bootstrap Tooltip显示换行和左对齐
- 预定义函数对象和函数适配器案例
- Node.js TTY模块
- JavaScript方法join()、splice()、slice()、split()
- IIS 试图加载格式不正确的程序的解决办法2
- 完成工具
- C++ 基本内置数据类型
- 导航上面的相关操作
- MySQL foreign key的几个注意点
- OpenResty基于nginx的限流插件