1. 预定义函数对象

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

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

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

#include <functional>

eg:

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

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

predefinedFuncObjectTest.cpp

deque<int> coll = { , , , , , , , ,  };

PRINT_ELEMENTS(coll, "initialized: ");

// negate all values in coll
transform(coll.cbegin(), coll.cend(), // source
coll.begin(), // destination
negate<int>()); // operation
PRINT_ELEMENTS(coll, "negated: "); // square all values in coll
transform(coll.cbegin(), coll.cend(), // first source
coll.cbegin(), // second source
coll.begin(), // destination
multiplies<int>()); // operation
PRINT_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<int, greater<int>> coll1 = { , , , , , , , ,  };
deque<int> 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 10
transform(coll1.cbegin(), coll1.cend(), // source
back_inserter(coll2), // destination
bind(multiplies<int>(), _1, )); // operation
PRINT_ELEMENTS(coll2, "transformed: "); // replace value equal to 70 with 42
replace_if(coll2.begin(), coll2.end(), // range
bind(equal_to<int>(), _1, ), // replace criterion
); // new value
PRINT_ELEMENTS(coll2, "replaced: "); // remove all elements with values between 50 and 80
coll2.erase(remove_if(coll2.begin(), coll2.end(),
bind(logical_and<bool>(),
bind(greater_equal<int>(), _1, ),
bind(less_equal<int>(), _1, ))),
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 ----------------

C++ 11 - STL - 函数对象(Function Object) (下)的更多相关文章

  1. C++ 11 - STL - 函数对象(Function Object) (上)

    1. 定义 在STL中,可以把函数传递给算法,也可以把函数对象传递给算法. 那么,什么是函数对象呢? 我们来看下它的声明: class X { public: // define function c ...

  2. C++ 11 - STL - 函数对象(Function Object) (中)

    我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...

  3. PythonStudy——函数对象 Function object

    # 在python中,所有变量存放的值只要是地址,我们就称之为对象# -- 所有的变量都是用来存放地址的,所以都是对象# -- 存放整型的地址就是整型对象 | 存放函数的地址就是函数对象 | 存放文件 ...

  4. C++11多态函数对象包装器

    [C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...

  5. C++11新特性之八——函数对象function

    详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...

  6. 条款20 STL函数对象

    继承标准STL的函数对象 1: struct PopLess : public atd::binary_function<state,state,bool> 2: { 3: bool op ...

  7. C++STL 函数对象和谓词

    函数对象:重载函数调用操作符的类,其对象常称为函数对象. 函数对象属于类对象,能突破函数概念,保持类的状态 谓词: 一元函数对象:函数参数1个: 二元函数对象:函数参数2个: 一元谓词 函数参数1个, ...

  8. 函数对象与仿函数(function object and functor)

    part 1. 仿函数在STL组件中的关系 如下图: # 仿函数配合算法完成不同的策略变化. # 适配器套接仿函数. part 2. 仿函数介绍 传递给算法的“函数型实参”不一定得是函数,可以是行为类 ...

  9. 认识js函数对象(Function Object)

    认识函数对象(Function Object) 可以用function关键字定义一个函数,对于每个函数可以为其指定一个函数名,通过函 数名来进行调用.这些都是代码给用户的印象,而在JavaScript ...

随机推荐

  1. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 初识JVM

    做了这么久的开发,到目前为止对JVM也只是一些简单的概念上的理解,正好周末于是将JVM的学习提上日程. JVM 概念 JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是 ...

  3. 【BZOJ 3294】 3294: [Cqoi2011]放棋子 (DP+组合数学+容斥原理)

    3294: [Cqoi2011]放棋子 Description Input 输入第一行为两个整数n, m, c,即行数.列数和棋子的颜色数.第二行包含c个正整数,即每个颜色的棋子数.所有颜色的棋子总数 ...

  4. 主席树+dfs SPOJ BZOJ2588 Count on a tree

    这道题我由于智障错误导致一直错. 在树上建主席树,加上lca思想,很简单. #include<bits/stdc++.h> using namespace std; ; struct no ...

  5. 如何使用Eclipse插件—Easy Explorer

    Easy Explorer是一个Eclipse插件,主要用于快速浏览项目文件的目录,实用性比较强. 使用方法如下: 1.下载Easy Explorer,从此处下载EasyExplorer:http:/ ...

  6. Codeforces Beta Round #10 B. Cinema Cashier 暴力

    B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...

  7. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) D. Delivery Bears 二分+网络流

    D. Delivery Bears 题目连接: http://www.codeforces.com/contest/653/problem/D Description Niwel is a littl ...

  8. jsonp和jsonpcallback的使用

    1. jsonp.jsonpCallback  jsonp跨域时可以自定义的两个参数 2. jsonp: 回掉函数名的参数名,默认callback,服务端通过它来获取到回掉函数名 3. jsonpCa ...

  9. 通过手机音频口,实现与单片机通讯,做电子签名成功n

    手机端的Ukey便携产品, 可以管理证书.加密解密.电子签名 : 1.通讯稳定,生成签名成功率100% 2.证书固化,私钥安全 3.走手机音频接口,通用.跨平台 4.耗电少,自带电池可长期供电,且可充 ...

  10. matlab colormap

    This table lists the built-in colormaps functions. Colormap Name Color Scale parula