82 stl(二)
2016-04-10 11:06
375 查看
类函数
#include<iostream> using namespace std; class FunObj { public: void operator()() { cout << "hello!" << endl; } void operator()(int n) { cout << "hello2!" << endl; } }; int main(void) { FunObj fo; fo(); fo(1); FunObj()(); return 0; }
Vector
#include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; void PrintFun(int n) { cout << n << ' '; } void Add3(int &n) { n += 3; } class PrintObj { public: void operator()(int n) { cout << n << ' '; } }; class AddObj { public: AddObj(int number) :number_(number) { } void operator()(int& n) { n += number_; } private: int number_; }; class GreaterObj { public: GreaterObj(int number) :number_(number) { } bool operator()(int n) { return n > 3; } private: int number_; }; int main(void) { int a[] = {1,2,3,4,5}; vector<int> v(a,a+5); for_each(v.begin(), v.end(), PrintFun); cout << endl; for_each(v.begin(), v.end(), PrintObj()); cout << endl; for_each(v.begin(), v.end(), Add3); for_each(v.begin(), v.end(), PrintFun); cout << endl; for_each(v.begin(), v.end(), AddObj(5)); for_each(v.begin(), v.end(), PrintFun); cout << endl; cout << count_if(a, a + 5, GreaterObj(3)) << endl; return 0; }
Map
#include <map> #include <string> #include <iostream> using namespace std; struct MyGreater { bool operator()(int left, int right) { return left > right; } }; int main(void) { map<int, string,MyGreater>mapTest; mapTest.insert(map<int, string>::value_type(1, "aaaa")); mapTest.insert(map<int, string>::value_type(3, "cccc")); mapTest.insert(map<int, string>::value_type(2, "bbbb")); for (map<int, string,MyGreater>::const_iterator it = mapTest.begin(); it != mapTest.end(); ++it) { cout << it->first << " " << it->second << endl; } return 0; }
相关文章推荐
- php常用的类函数和对象函数
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
- 类声明和成员函数定义分离
- 【剑指offer系列】 字符串排列___28
- Error running app:instant Run requires "Tools | Android | Enable ADB integration to be enable
- HDU 4618 Palindrome Sub-Array 暴力
- jvm类加载器的父类委托机制
- Bootstrap入门
- java接口和抽象类
- MFC基础:Windows内部程序运行原理
- 数据库表空间使用率查询
- asp.net mvc中自行搭建实现工厂模式的spring.net+三层架构 (15-4-20)
- Citrix Director
- Java虚拟机结构--堆栈
- Log4j配置详解
- javascript原生ajax写法分享
- 整理:Integer与int的比较
- decltype操作符
- java eclipse 反编译插件安装
- decltype操作符