STL_算法_中使用的函数对象
2016-03-08 16:28
483 查看
写在前面:
STL算法中的 函数对象的功能:
(1)、都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系,
(2)、返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往下进行,返回true==>继续下一组相邻元素的比较,返回false==>中断下一组相邻元素的比较。
1、
例如:
binary_search(?, ?, ?, ?);
中的 第4个参数 是一个函数对象。
然后,第4个参数 可以传入下面3中样式的值:
1.1、
类似这样:
可以这样写:binary_search(?, ?, ?, CompareInt);
ZC: 这里直接传的是 函数指针
1.2、(C++ 中 struct 和 class 是一回事)
1.2.1、
类似这样:
可以这样写:binary_search(?, ?, ?, CompareFunctor());
ZC: 这里传的是 struct的对象
1.2.2、
类似这样:
可以这样写:binary_search(?, ?, ?, CompareFunctor());
ZC: 这里传的是 类对象
2、
STL算法中的 函数对象的功能:
(1)、都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系,
(2)、返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往下进行,返回true==>继续下一组相邻元素的比较,返回false==>中断下一组相邻元素的比较。
1、
例如:
binary_search(?, ?, ?, ?);
中的 第4个参数 是一个函数对象。
然后,第4个参数 可以传入下面3中样式的值:
1.1、
类似这样:
bool CompareInt (const int& _iLeft, const int& _iRight) { return _iLeft < _iRight; }
可以这样写:binary_search(?, ?, ?, CompareInt);
ZC: 这里直接传的是 函数指针
1.2、(C++ 中 struct 和 class 是一回事)
1.2.1、
类似这样:
struct CompareFunctor { public: bool operator() (const int& _iLeft, const int& _iRight) { return (_iLeft < _iRight); } };
可以这样写:binary_search(?, ?, ?, CompareFunctor());
ZC: 这里传的是 struct的对象
1.2.2、
类似这样:
class CompareFunctor { public: bool operator() (const int& _iLeft, const int& _iRight) { return (_iLeft < _iRight); } };
可以这样写:binary_search(?, ?, ?, CompareFunctor());
ZC: 这里传的是 类对象
2、
相关文章推荐
- C++ 用libcurl库进行http通讯网络编程(转)
- 无Root抓包:Packet Capture 1.2.3
- Atitit.木马 病毒 免杀 技术 360免杀 杀毒软件免杀 原理与原则 attilax 总结
- linux常见命令
- MySQL主从复制的一些错误处理和日常维护
- zTree 简单使用
- Node.js笔记之简介
- js实现数组冒泡排序、快速排序原理
- java和php实现RSA加密互通
- iOS App 后台运行调研
- 虚拟机安装Ubuntu不能全屏解决方法
- Android Fragment 真正的完全解析(下)
- 上传excel文件
- Codeforces A. Orchestra
- C++编程注意的一些细节(一直更新)
- 打开桌面word文件,出现乱码解决办法(未完待续)
- Linux中zip压缩和unzip解压缩命令详解
- 133.Which two statements are true regarding subqueries? (Choose two.)
- JS代码判断IE6,IE7,IE8,IE9的函数代码
- atitit.TokenService token服务模块的设计