博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 11 - STL - 函数对象(Function Object) (下)
阅读量:7238 次
发布时间:2019-06-29

本文共 2369 字,大约阅读时间需要 7 分钟。

1. 预定义函数对象

C++标准库内含许多预定义的函数对象,也就是内置的函数对象。

你可以充分利用他们,不必自己费心去写一些自己的函数对象。

要使用他们,你只要包含如下头文件

#include <functional>

eg:

set<int, less<int>> coll;  // sort elements with <

set<int, greater<int>> coll;  // sort elements with >

predefinedFuncObjectTest.cpp

deque
coll = { 1, 2, 3, 5, 7, 11, 13, 17, 19 };PRINT_ELEMENTS(coll, "initialized: ");// negate all values in colltransform(coll.cbegin(), coll.cend(), // source coll.begin(), // destination negate
()); // operationPRINT_ELEMENTS(coll, "negated: ");// square all values in colltransform(coll.cbegin(), coll.cend(), // first source coll.cbegin(), // second source coll.begin(), // destination multiplies
()); // operationPRINT_ELEMENTS(coll, "squared: ");

运行结果:

---------------- predefinedFuncObject(): Run Start ----------------

initialized: 1 2 3 5 7 11 13 17 19
negated:     -1 -2 -3 -5 -7 -11 -13 -17 -19
squared:     1 4 9 25 49 121 169 289 361
---------------- predefinedFuncObject(): Run End ----------------

 

2. 预定义函数对象绑定

你可以使用binder将预定义函数对象和其他数值进行绑定。

pdFuncObjectBind.cpp

using namespace std::placeholders;set
> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };deque
coll2;// Note: due to the sorting criterion greater<>() elements have reverse order:PRINT_ELEMENTS(coll1, "initialized: ");// transform all elements into coll2 by multiplying them with 10transform(coll1.cbegin(), coll1.cend(), // source back_inserter(coll2), // destination bind(multiplies
(), _1, 10)); // operationPRINT_ELEMENTS(coll2, "transformed: ");// replace value equal to 70 with 42replace_if(coll2.begin(), coll2.end(), // range bind(equal_to
(), _1, 70), // replace criterion 42); // new valuePRINT_ELEMENTS(coll2, "replaced: ");// remove all elements with values between 50 and 80coll2.erase(remove_if(coll2.begin(), coll2.end(), bind(logical_and
(), bind(greater_equal
(), _1, 50), bind(less_equal
(), _1, 80))), coll2.end());PRINT_ELEMENTS(coll2, "removed: ");

运行结果:

---------------- pdFuncObjectBind(): Run Start ----------------

initialized: 9 8 7 6 5 4 3 2 1
transformed: 90 80 70 60 50 40 30 20 10
replaced:    90 80 42 60 50 40 30 20 10
removed:     90 42 40 30 20 10
---------------- pdFuncObjectBind(): Run End ----------------

 

转载于:https://www.cnblogs.com/davidgu/p/4846327.html

你可能感兴趣的文章
系统集成项目管理工程师软考辅导——3年真题透解与全真模拟
查看>>
以安装桌面体验功能为例来探索windows2012服务器管理器的新变化
查看>>
ubuntu安装与测试hadoop1.1.0版本
查看>>
运维开发实战考题:计算教育网站投票排名
查看>>
Lync 小技巧-54-Elastix IVR 转换Wav方法
查看>>
JAVAEE知识的系统性有多重要?再谈非线性学习方法
查看>>
Yii2 HOW-TO(3):调试工具yii2-debug和Xdebug(失败)
查看>>
PC上安装MAC X Lion
查看>>
大共享永久免费云服务器评测体验
查看>>
虚拟化基础架构Windows 2008篇之4-将Windows计算机加入到域
查看>>
about script engine on jdk 6 is mozilla rhino
查看>>
android之Apache Http初使用——向服务器发送请求
查看>>
nohup命令 使用方法详解
查看>>
Intent 和 Intent Filter
查看>>
Linux下MySQL的主从热备(自动同步)配置
查看>>
Windows 8 Consumer Preview版升级到 Release Preview 版后Metro应用(html5+JavaScript版)修改小结...
查看>>
编程珠玑:变位词程序的实现
查看>>
POJ 2987 Firing
查看>>
Newtonsoft.Json 应用
查看>>
HDU 1400 Mondriaan's Dream
查看>>