当前位置:网站首页>[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 :
边栏推荐
- Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
- Brief introduction of emotional dialogue recognition and generation
- Go language sqlx library operation SQLite3 database addition, deletion, modification and query
- STM32之ADC
- Deep analysis of data storage in memory - C language
- Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
- QT qpprogressbar details
- The use of 8255 interface chip and ADC0809
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- 内网渗透 | 手把手教你如何进行内网渗透
猜你喜欢

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

Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image

基于Pyqt5工具栏按钮可实现界面切换-2

CDN acceleration requires the domain name to be filed first

程序员版本的八荣八耻~

Value sequence < detailed explanation of daily question >

ADC of stm32

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

CDN 加速,需要域名先备案

MySQL queries nearby data And sort by distance
随机推荐
Li Kou brush questions (2022-6-28)
Hisilicon VI access video process
The use of 8255 interface chip and ADC0809
Submit code process
Pytorch training CPU usage continues to grow (Bug)
跨境电商如何通过打好数据底座,实现低成本稳步增长
2022年最新最全软件测试面试题大全
Deep analysis of data storage in memory - C language
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
2016. maximum difference between incremental elements
Potplayer set minimized shortcut keys
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Cryptography -- the mode of block cipher
Win11麦克风测试在哪里?Win11测试麦克风的方法
Solving ordinary differential equations with MATLAB
在SOUI里使用真窗口时使用SOUI的滚动条
20220524_数据库过程_语句留档
MarkDown基本语法
SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常