当前位置:网站首页>[analysis of STL source code] imitation function (to be supplemented)
[analysis of STL source code] imitation function (to be supplemented)
2022-07-02 23:23:00 【Cloudeeeee】
【STL Source analysis 】 functor ( To be added )
7.1 functor (fuctors) An overview
Function of imitative function :
STL Various algorithms provided in , There are often two versions , One is the most intuitive default operation , The second is to run users with “template Parameter to specify the policy to be adopted ” . for example accumulate(), The first version is to add all the elements in the specified range , The second version can pass in the user specified operation , To replace this addition process .
This specified operation should be passed into the algorithm as a parameter of the algorithm , The only way is to design the operation as a function , then A function pointer As a parameter of the algorithm ; Or design the operation as a functor ( It's essentially a class object ), Then create an object with this functor and pass it into the function as the parameter of the function .
The essence of a functor is an object that behaves like a function , In order to simulate the function call , Functors must be overloaded function call Operator , That is, parentheses 、operator(), So we can add (), Thus, we can call the operator().
STL Classification of affine functions :
- According to the number of operands : One yuan 、 binary
- According to the function : Arithmetic operations 、 Relationship between operation 、 Logical operations
Any program that wants to use STL Built in affine function , Must contain < functional > The header file .
How to use the functor :

7.2 Can be matched with (Adaptable) The key to
STL A functor should have a function adapter (function adapter) Ability to modify , Therefore, each functor must define its own corresponding type , Just like the five corresponding types defined by the iterator , These corresponding types are to enable the adapter to get some information of the functor . The corresponding types are typedef operation , This operation is completed at compile time , It will not affect the execution efficiency of the program , Without any additional burden .
The corresponding types of functors are mainly used to represent Function parameter type and Return value type ,< stl_function.h > The base classes of univariate and bivariate affine functions are defined respectively unary_function、binary_function, Any affine function should inherit one of them according to personal needs , They automatically have those corresponding types , It will automatically have the matching ability .
7.2.1 unary_function
unary_function Used to present the parameter type and return value type of unary functor :

7.2.2 binary_function
binary_function The type of the first parameter used to render a bivariate functor 、 The type of the second parameter and the return value type :
边栏推荐
- ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
- 20220524_数据库过程_语句留档
- Submit code process
- Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
- Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
- 20220524_ Database process_ Statement retention
- 【直播预约】数据库OBCP认证全面升级公开课
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
- 高数有多难?AI 卷到数学圈,高数考试正确率 81%!
猜你喜欢

Go language sqlx library operation SQLite3 database addition, deletion, modification and query

Redis 过期策略+conf 记录

Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs

Catalogue of digital image processing experiments

Value sequence < detailed explanation of daily question >

MySQL queries nearby data And sort by distance

密码技术---分组密码的模式

Methods to solve the tampering of Chrome browser and edeg browser homepage

跨境电商如何通过打好数据底座,实现低成本稳步增长

Detailed explanation and application of merging and sorting
随机推荐
设置单击右键可以选择用VS Code打开文件
Go project operation method
Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image
ServletContext learning diary 1
How does win11 turn on visual control? Win11 method of turning on visual control
公司里只有一个测试是什么体验?听听他们怎么说吧
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Application of containerization technology in embedded field
抖音实战~点赞数量弹框
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
内网渗透 | 手把手教你如何进行内网渗透
Quantitative analysis of PSNR, SSIM and RMSE
2016. maximum difference between incremental elements
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Print out mode of go
Win11如何开启目视控制?Win11开启目视控制的方法
[favorite poems] OK, song
[hardware] origin of standard resistance value
(stinger) use pystinger Socks4 to go online and not go out of the network host