当前位置:网站首页>折叠表达式
折叠表达式
2022-06-11 13:25:00 【发如雪-ty】
先看看一个例子,这个例子中有三个求和函数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;
}
结果:
上述代码中sum3就使用了折叠表达式,可以看到代码非常简洁,却也实现了相同的功能。
折叠表达式是c++17标准引入的,引入折叠表达式的主要目的是计算某个值,这个值的特殊在于:它与所有可变参有关,而不是于单独某个参数有关。
折叠表达式一般有4种格式(注意每种格式都是用圆括号括起)。左折就是参数从左侧开始计算,右折就是参数从右侧开始计算。
一元左折
格式:(… 运算符 一包参数)
计算方式:(((参数1 运算符 参数2)运算符 参数3))…运算符 参数N)
示例:
template<typename ...U>
auto sub_left(U...paras)
{
return (... - paras);
}
int main()
{
cout << sub_left(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
结果:-80
一元右折
格式:(一包参数 运算符 …)
计算方式:参数1 运算符(参数2…(参数N-1 运算符 参数N))
示例:
template<typename ...U>
auto sub_right(U...paras)
{
return (paras - ...);
}
int main()
{
cout << sub_right(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
结果:-20
二元左折
格式:(init 运算符 … 运算符 一包参数)
计算方式:(((init 运算符 参数1)运算符 参数2)…运算符 参数N)
示例:
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;
}
结果:120
((((220-10)-20)-30)-40)
二元右折
格式:(一包参数 运算符 … 运算符 init)
计算方式:(参数1运算符(…(参数N 运算符 init))
示例:
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;
}
结果:200
10-(20-(30-(40-220)))=200
边栏推荐
- 关于uni-app 配置 APP 不显示顶部标题栏设置
- Today in history: Apple II comes out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born
- 如何写出高性能代码(四)优化数据访问
- kubernetes 二进制安装(v1.20.16)(五)验证 master 部署
- 火山引擎云数据库 veDB 在字节内部的业务实践
- BS-XX-007基于JSP实现户籍管理系统
- Pki/tls Swiss Army knife cfssl
- 想要实现在时序场景下“远超”通用数据库,需要做到哪几点?
- [bug resolution] after uploading the image, cancel the upload and upload again. The last image still exists
- From real-time computing to streaming data warehouse, where will Flink go next?
猜你喜欢

Why does each running Department display that the database already exists, delete the database, and then succeed? Each running department must delete the database, and re run the whole successfully

Is byte really the end of the universe?

马斯克称自己不喜欢做CEO,更想做技术和设计;吴恩达的《机器学习》课程即将关闭注册|极客头条...

火山引擎云数据库 veDB 在字节内部的业务实践

No delay / no delay live video instance effect cases

苹果将造搜索引擎?

Some transformation thoughts of programmers after they are 35 years old

Introduction to common fonts

关于uni-app 配置 APP 不显示顶部标题栏设置

Can't understand kotlin source code? Starting with the contracts function~
随机推荐
[bug resolution] after uploading the image, cancel the upload and upload again. The last image still exists
C # set the cursor shape of forms and systems
字节真的是宇宙尽头吗?
Live share experience
What do you need to do to "surpass" the general database in the time sequence scenario?
JDBC连接池去做批量导入,每次运行500万数据,但是在中间就会出各种问题
InfoQ 极客传媒 15 周年庆征文|移动端开发之动态排行【MUI+Flask+MongoDB】
不谈赛道,不聊风口,开源数据库巨头Cassandra如何在国内讲好“新故事” | C位面对面
关于uni-app 配置 APP 不显示顶部标题栏设置
The Tree (AVL, 2-3-, 红黑,Huffman)
三级分类展示
Nomad application layout scheme 07 of hashicopy (submit job)
Bs-xx-007 registered residence management system based on JSP
InfoQ geek media's 15th anniversary essay solicitation - dynamic ranking of mobile terminal development [mui+flask+mongodb]
2022工具钳工(中级)操作证考试题库及答案
SQL: how to use the data of purchase order and sales order to calculate commodity cost by moving weighted average method
Hashicopy之nomad应用编排方案07(提交Job)
[interface] view the interface path and check the interface
[the path of system analyst] collection of wrong topics of system analyst
Is byte really the end of the universe?