当前位置:网站首页>[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 :
边栏推荐
- CDN acceleration requires the domain name to be filed first
- [Solved] Splunk: Cannot get username when all users are selected“
- 基于Pyqt5工具栏按钮可实现界面切换-2
- 采用VNC Viewer方式远程连接树莓派
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- 万物并作,吾以观复|OceanBase 政企行业实践
- 数字图像处理实验目录
- Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
- STM32之ADC
- Application of containerization technology in embedded field
猜你喜欢
Three solutions to frequent sticking and no response of explorer in win11 system
Eight honors and eight disgraces of the programmer version~
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
Set right click to select vs code to open the file
程序员版本的八荣八耻~
Hisilicon VI access video process
Li Kou brush questions (2022-6-28)
What can I do after buying a domain name?
Detailed explanation and application of merging and sorting
聊聊内存模型与内存序
随机推荐
Numerical solution of partial differential equations with MATLAB
门牌制作 C语言
Golang common settings - modify background
Arduino - 字符判断函数
抖音实战~点赞数量弹框
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
力扣刷题(2022-6-28)
RecyclerView结合ViewBinding的使用
用matlab调用vs2015来编译vs工程
The use of 8255 interface chip and ADC0809
Arduino - character judgment function
Which common ports should the server open
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Hisilicon VI access video process
详解Promise使用
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
公司里只有一个测试是什么体验?听听他们怎么说吧
深度剖析数据在内存中的存储----C语言篇
Makefile configuration of Hisilicon calling interface
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022