当前位置:网站首页>Collapse expression
Collapse expression
2022-06-11 13:30:00 【Hair like snow ty】
Let's take a look at an example , There are three summation functions in this example sum1,sum2,sum3.
auto sum1()
{
return 0;
}
template<typename T, typename...U>
auto sum1(T a, U... rest)
{
return a + sum1(rest...);
}
template<typename T,typename...U>
auto sum2(T a, U... rest)
{
if constexpr(sizeof...(rest)>0)
{
return a + sum2(rest...);
}
else
{
return a;
}
}
template<typename...U>
auto sum3(U... rest)
{
return (... + rest);
}
int main()
{
cout << sum1(1, 2, 3, 5.5, 6) << endl;
cout<<sum2(1, 2, 3, 5.5, 6) << endl;
cout << sum3(1, 2, 3, 5.5, 6) << endl;
system("pause");
return 0;
}
result :
In the above code sum3 You use the collapse expression , You can see that the code is very concise , But it also realizes the same function .
The collapse expression is c++17 Standard introduced , The main purpose of introducing folding expression is to calculate a certain value , What makes this value special is : It relates to all variable parameters , It is not related to a single parameter .
Folding expressions generally have 4 format ( Note that each format is enclosed in parentheses ). Left fold means that parameters are calculated from the left , Turning right means that parameters are calculated from the right .
One yuan is off to the left
Format :(… Operator A package of parameters )
Calculation method :((( Parameters 1 Operator Parameters 2) Operator Parameters 3))… Operator Parameters N)
Example :
template<typename ...U>
auto sub_left(U...paras)
{
return (... - paras);
}
int main()
{
cout << sub_left(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
result :-80
One dollar right discount
Format :( A package of parameters Operator …)
Calculation method : Parameters 1 Operator ( Parameters 2…( Parameters N-1 Operator Parameters N))
Example :
template<typename ...U>
auto sub_right(U...paras)
{
return (paras - ...);
}
int main()
{
cout << sub_right(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
result :-20
Binary left fold
Format :(init Operator … Operator A package of parameters )
Calculation method :(((init Operator Parameters 1) Operator Parameters 2)… Operator Parameters N)
Example :
template<typename ...U>
auto sub_left(U...paras)
{
return (220 - ... - paras);
}
int main()
{
cout << sub_left(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
result :120
((((220-10)-20)-30)-40)
Two yuan right turn
Format :( A package of parameters Operator … Operator init)
Calculation method :( Parameters 1 Operator (…( Parameters N Operator init))
Example :
template<typename ...U>
auto sub_right(U...paras)
{
return (paras - ... - 220);
}
int main()
{
cout << sub_right(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
result :200
10-(20-(30-(40-220)))=200
边栏推荐
- China's SaaS development lags behind that of the United States for 10 years, and it still needs to rely on innovation, open source, M & A and other strategies | archsummit
- Jdbctemplate data background management. I don't know why roleid is empty when adding users
- 关于uni-app 配置 APP 不显示顶部标题栏设置
- Introduction to long connection
- Optimal dispatching (thermal power, wind and energy storage) (realized by Matlab)
- 【201】php异常处理-PHP中的try catch finally异常处理
- Can't understand kotlin source code? Starting with the contracts function~
- d区间到可空转换
- create_ Error in engine MySQL connector encryption method
- Customize terrain providers (terrain plugin framework) -04
猜你喜欢

Teatalk · online speech record | complete! It is important to select the right data migration strategy for safe cloud deployment

六.开发记录之实验室服务器LXD部署

三级分类展示

面试造航母,入职拧螺丝,工资...
![[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers](/img/7f/8ffc83e5717275b27f1fd34111ca15.png)
[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers

为什么每运行一部都显示一次数据库已存在,都要删除数据库,然后才能成功,每运行一部都要删除一次数据库,重新运行整体才成功.

详解C语言实参与形参的区别

On the continuing Life of Distributed Locks - - Distributed Locks Based on redis

Operating instructions for communication between RS485 (Modbus RTU) industrial RFID reader ck-fr03-a01 and PLC Mitsubishi fx5u

Introduction to common fonts
随机推荐
Unity detects whether the object is within the viewing cone of the camera
【201】php异常处理-PHP中的try catch finally异常处理
Go 如何减少供应链攻击?
@How to resolve controller and requestmapping
shader着色器
SAP Spartacus 中的 checkout(结帐) 设计
Hashicopy之nomad应用编排方案07(提交Job)
Explanation of waitgroup usage in go language learning
Is byte really the end of the universe?
2022 年,捕捉这 12 个数据和分析趋势!
【信号处理】数字信号处理Matlab设计附GUI界面和报告
[acwing 11. solution number of knapsack problem] 01 knapsack + 01 knapsack + understand the specific meaning of 01 knapsack
历史上的今天:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生...
全球手机市场衰退,连苹果也对iPhone14不抱过高期待
TeaTalk·Online 演讲实录 | 圆满完结!安全上云,选对数据迁移策略很重要
AGV机器人RFID传感器CK-G06A与西门子1200PLC应用手册
Microsoft exposes another "scandal": watching VR porn in the office, "the father of hololens" is about to leave!
Jdbctemplate data background management. I don't know why roleid is empty when adding users
Can't understand kotlin source code? Starting with the contracts function~
[arcgis] City relevance analysis