C++ 11 - STL - 函数对象(Function Object) (下)

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 = { 1, 2, 3, 5, 7, 11, 13, 17, 19 };

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 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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, 10));   // operation
PRINT_ELEMENTS(coll2, "transformed: ");

// replace value equal to 70 with 42
replace_if(coll2.begin(), coll2.end(),       // range
    bind(equal_to<int>(), _1, 70),     // replace criterion
    42);                             // 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, 50),
    bind(less_equal<int>(), _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 ----------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL是指标准模板库(Standard Template Library),它是C++语言的一部分,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的目标是提供高效、可重用和可扩展的组件,以便开发人员能够更轻松地编写高质量的代码。STL包含了许多常见的数据结构,如vector、list、set、map等,以及各种算法,比如排序、查找、遍历等。通过使用STL,开发人员可以更加高效地处理各种数据结构和算法的问题,提高代码的开发效率和质量。 在STL中,我们可以使用各种容器来存储和管理数据。例如,我们可以使用std::map来创建一个键值对的映射,其中每个键都有一个与之相关联的值。下面是一个示例代码,展示了如何创建和使用一个std::map对象: std::map<std::string, int> disMap() { std::map<std::string, int> tempMap{ {"C语言教程",10},{"STL教程",20} }; return tempMap; } std::map<std::string, int> newMap(disMap()); 在这个示例中,disMap()函数创建了一个临时的std::map对象,并初始化了其中的一些键值对。然后,使用移动构造函数将这个临时对象移动到了一个新的std::map对象newMap中。最终,我们可以通过newMap对象来访问和操作这些键值对。 综上所述,STLC++中的标准模板库,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的使用可以提高代码的开发效率和质量,并且通过各种容器和算法,可以方便地处理各种数据结构和算法的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL详解超全总结(快速入门STL)](https://blog.csdn.net/qq_50285142/article/details/114026148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C++实验】阅读STL源码并分析](https://blog.csdn.net/qq_35760825/article/details/125311509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值