当前位置:网站首页>Variable parameter template implements max (accepts multiple parameters, two implementation methods)
Variable parameter template implements max (accepts multiple parameters, two implementation methods)
2022-06-24 11:34:00 【Junmo vv】
Imitative function implementation and recursive implementation
#include <iostream>
struct __Iter_less_iter
{
template<typename iteraotr1,typename iterator2>
bool operator() (iteraotr1 it1, iterator2 it2)
{
return *it1 < *it2;
}
};
__Iter_less_iter __iter_less_iter()
{
return __Iter_less_iter();
}
template<typename Iterator,typename comp>
Iterator __max_element(Iterator first, Iterator last, comp cmp)
{
if (first == last) {
return first;
}
Iterator reslut = first;
while (++first != last) { //last Is to point to the next position of the last element
if (cmp(reslut, first)) {
reslut = first;
}
}
return reslut;
}
template<typename T>
T max_element(T first,T last)
{
return __max_element(first, last, __iter_less_iter());
}
template <typename T>
T max(std::initializer_list<T> L)
{
return *max_element(L.begin(), L.end());
}
template <typename T>
T maxnum(T& in)
{
return in;
}
template <typename T,typename... Args>
T maxnum(T first, Args... args)
{
return std::max(first, maxnum(args...));
}
int main()
{
std::cout << max({ 23,42,44,22,11 }) << std::endl;
std::cout << maxnum( 23,42,44,22,11 ) << std::endl;
return 0;
}
边栏推荐
- d的10个0符
- Basic path test of software test on the function of the previous day
- Why choose b+ tree as storage engine index structure
- 集群控制管理
- First acquaintance with string+ simple usage (I)
- Influence of DEX optimization on arouter lookup path
- Example of PHP observer mode [useful in the laravel framework]
- 脚本之美│VBS 入门交互实战
- Anonymous Messenger: hidden communication of Trojan horse
- 《opencv学习笔记》-- 图像的载入和保存
猜你喜欢
随机推荐
如何只导出word文档中的标题?(即将正文内容都删除,只保留标题)B站牛逼
How to improve the quality of Baidu keyword?
math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/
Which is a good CAD drawing software? How to select good software
万名校园开发者花式玩AI,亮点看这张图就够啦!
怎么申请打新债 开户是安全的吗
Axi low power interface
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
qt -- QTabWidget 中支持拖拽TabBar项
Google Earth Engine(GEE)—如何新增一个图例在Map面板
Group policy export import
How to develop hospital information system (his) with SMS notification and voice function
Why are some old SEO methods still effective?
Reading at night -- about microservices and containers
Ppt drawing related, shortcut keys, aesthetics
集群控制管理
Any and typevar make the automatic completion of IDE better
How to use arbitrarygen code generator what are the characteristics of this generator
Beauty of script │ VBS introduction interactive practice
How does wechat and QQ chat work? So simple!!!