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. css去除chrome下select元素默认border-radius

    在mac下的chrome,对于select元素会默认有一个border-radius,当然有些情况下并不需要圆角,所以就要去掉. 比较常用的方法是: .select { -webkit-appeara ...

  2. 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)

    3997: [TJOI2015]组合数学 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 919  Solved: 664 Description 给出 ...

  3. 一个安卓应用 多少个 dalvik 虚拟机

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 一个

  4. CodeForces 606C Sorting Railway Cars(最长连续上升子序列)

    Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...

  5. hihoCoder #1695 公平分队II

    题目大意 Alice 和 Bob 在玩一个游戏.Alice 将 $1$ 到 $2n$ 这 $2n$ 个整数分成两组,每组 $n$ 个.Bob 从中选一组,剩下一组归 Alice.Alice 可以与 B ...

  6. UESTC 2015dp专题 E 菲波拉契数制 dp

    菲波拉契数制 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descr ...

  7. The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (GameV

    在当前短信内容的activity中写            Bundle bun = new Bundle();         bun.putString("message",  ...

  8. Low-cost ADC using only Digital I/O

    http://letsmakerobots.com/node/13843 Reading A Sensor With Higher Accuracy – RC Timing Method RC Tim ...

  9. TRF7970A IC Communication Interface

    General Introduction The communication interface to the reader can be configured in two ways: with a ...

  10. Spring MapFactoryBean例子

    MapFactoryBean类为开发者提供了一种在Spring的bean配置文件中创建一个具体的Map集合类(HashMap和TreeMap). 这里有一个MapFactoryBean.例如,在运行时 ...